As mentioned earlier in the chapter, the first use case revolves around a stock portfolio use case for demonstrating publ Ish/subscribe Messaging. This example are simple and utilizes a Publisher class for sending the stock price messages to a topic, as well as a Consumer Class for registering a Listener class to consume messages from topics on an asynchronous manner. These three classes embody the functionality of generating ever-changing stock prices that is published to topics O n which the consumer is subscribed.
As mentioned earlier in this chapter, the first use case is around the cycle of stock futures using this case to justify the publish/subscribe message. This case is simple and uses a Publisher class to send a stock price message to topic, and uses the consumer class registered with the listener class to consume messages from topic in an asynchronous pattern, containing the ability to generate time to transform the stock price function, These prices were posted on the subject, and the topics were subscribed by consumers.
In this example, the stock prices is published to an arbitrary number of topics. The number of topics is based on the number of arguments sent to the Publisher and the Consumer on the command line. Each class would dynamically send and receive to/from the topics (an example is provided next). Take a-look at figures 3.2 and 3.3-to-see at a high level, what the examples seek to achieve.
In this case, the stock price is posted to any number of subjects you enter, the number of subjects that are entered in the form of a command line for publisher and consumer, and each class is dynamically sent or accepted to the subject (the next instance is provided), glancing ahead, In the 3.2 and 3.3 instances, you will see how to complete the exploration at a high level.
for the sake of this demonstration, and the other topics would be used. The Publisher class uses a single JMS messageproducer to send 1,000 fictitious& nbsp stock price messages in blocks of ten, randomly distributed across the topics named in the command-line argumen T. After it sends $ messages, it shuts down. The Consumer class creates one JMS messageconsumer per topic and registers a JMS messagelistener for each topic. Because This example demonstrates publish/subscribe, the consumers must is online to consume messages being sent by the Pu Blisher, because durable consumers aren ' t used in the basic stock portfolio example. The next step is to actually run, the example so, can see them in action.
To verify this example, I used 2 topics, and the Publisher class used a single JMS message that the consumer sent 1000 stock market messages, a 10 message interval of 1000 milliseconds, and a randomly distributed cross-topic generated by the command line input at class startup. After sending 1000 messages, the producer closes. The consumer class generates a JMS message consumer, and a JMS message listener is registered for each topic. Because this instance is a validation of the publish/subscribe, consumption must be online to consume messages published by publisher, as there will be no persistent consumer in this most basic stock futures instance (the understanding should be that the consumer must first start the class to be explained), The next step is to actually run the example so you can see how it works.
3.2.1 Running stock futures portfolio instances
Basic steps:
1. Start ACTIVEMQ
2. Run Consumer class
3. Run the Publisher class
The step is simple, the code is also, the only thing you need to note is that the consumer class should start earlier than the Publisher class in order to wait for all messages that have just been published. The reason is that in this instance the publish/subscribe messages and topics are not caught by the message unless the consumer has set a persistent subscription here. So let's start with this example of stock futures.
The first task is to execute the boot activemq. Don't be silent, take a rest and sleep. Here is a diagram of my example understanding:
The following instructions are helpful for understanding the program:
1, the Publisher only 1, 2 consumers, the message 1 to multi-mode.
2. This publisher closes the connection after sending 10 principal messages.
3, the theme topic created process, fixed format:
① is created by a total of one session to create a different name for the publishing topic topic.
② creates a mapping message that is customized to be able to wrap up the data that you need.
③ eventually sent this mapping message by the producer to complete a topic release.
The publishers that need to be stressed here are different from the consumer, and for each message, the publisher needs to re-customize the destination and publish the topic for this message, and the instant desination and release topics are the same as the previous message. So, in my program, the publisher created 10 publishing topics and sent 10 messages. And in the consumer, just created (subscribed) 2 topics, this is essentially different.
For the publisher, in fact, is the producer, the producer is the performer of the action, but it sends the message is no longer ordinary message, this message will enter the ACTIVEMQ JMS message domain, that is, into the queue or topic, such messages more like a more advanced message with the routing function. Anyway, I understand that, I don't know, right?
The disadvantage of this program: here I just to verify the publish/subscribe model, the published topic will not catch the message unless the consumer has set a persistent subscription. From the point of view of the program, when the publisher publishes the message, many online consumers will immediately get the message of these subscriptions, while not the line consumers immediately online, the past release topic has gone, not get. This will be talked about in the future.
Section 3.2 use Case 1: Stock futures stocks portfolio 21050917