In this lab, you will receive an example app that uses the Observer pattern to isolate the creation of orders from two processes that summarize these orders. You will refactor the codebase to use the pub-sub pattern to allow the two observers to each occur within their own subscriber, so they could, in theory, each run on their own nodes.
One of the observers computes a customer’s total purchasing. Another one of the observers reports how long it will take to ship to a given destination.
In the orders package, the Store class has a main method. When you run this, it should start to produce random orders and the observers should print their summaries on the orders to the console.
In the pubsub package you will find example programs that connect to sdlstudentvm03.msoe.edu and send messages to each other through that server. Please check that you are able to use the Subscriber, then use the Publisher to send messages that are seen by the subscriber.
We have set up a nats server on sdlstudentvm03.msoe.edu so that you don’t need to configure your own server. This will also allow you to send messages between students. Please remember that NOTHING published to this server is private!
Divide the observers and subjects into their own subscribers and publishers. Your program should have three “Mains” – the Store, the CustomerTotalsSubscriber, and the ShippingTimeSubscriber. Refactor the pubsub code into these “Mains” and, when you are ready, delete the pubsub package.
Note that the NewOrderEvent has a toString
method that you can use to get a string to publish to nats. Rather than reconstructing the full order on the receiver (which is impossible), you can simply parse this string for what the receiver needs.
To test your program, first start both subscribers, which should connect to the nats server and wait for messages. Then launch the publisher, which should start sending order messages to the nats server.
When your program is complete, each subscriber should print just the messages that the corresponding observer printed, with exactly the same format that that observer used.
There is one observer used as a lambda expression on the publisher. This does NOT need to be implemented as a subscriber. Simply print each new order event as it occur on the publisher.
Push your code to Github Classroom. You may be asked to do a brief demo in the following week’s lab.