Introduction
In the previous article discussed several application system integration methods, found that the actual Message Queuing-oriented integration scheme is a more reasonable overall choice. Here, we first discuss a specific message queue ACTIVEMQ Basic communication method. ACTIVEMQ is an implementation of the JMS message Communication specification. In general, the most common types of message communication patterns defined in the message specification are publish-subscribe, point-to-point. In addition, by combining the specific applications of these patterns, we also derive a pattern of request responses when dealing with certain scenarios. Below, we discuss these several ways one by one.
Basic Process
When discussing specific ways, let's take a look at the main process of using ACTIVEMQ to start a service.
In accordance with the JMS specification, we first need to obtain a JMS connection factory. Connection is created through this connection factory. On this basis we create the session, destination, Producer and consumer. So the main steps are as follows:
1. Obtain the JMS connection factory. Construct factory by providing connection information for specific environments.
2. Structuring the JMS connection with factory
3. Start connection
4. Create a JMS session via connection.
5. Specify the JMS destination.
6. Create a JMS producer or create a JMS message and provide destination.
7. Create a JMS consumer or register JMS message listener.
8. Send and receive JMS message.
9. Close all JMS resources, including connection, session, producer, consumer, etc.
Publish-subscribe
The Publish subscription model is a bit like subscribing to newspapers in our daily lives. At the end of each year, the post Office sends a collection of newspapers. Let's choose which one to subscribe to. All the published newspapers are listed in the table, so we can choose one or more newspapers for each of our subscribers. such as Beijing Daily, Xiaoxiang morning news and so on. Then the newspapers we subscribe to are the equivalent of topic in the subscription model. There are many individuals who subscribe to newspapers, and others may subscribe to the same newspaper. Well, here, it's equivalent to registering in the same topic. For a newspaper issuer, it has a 1-to-many relationship with all subscribers. This relationship is shown in the following figure:
Now, suppose we write a simple example using the scenario discussed earlier. The first thing we need to define is publisher. Publisher
Publisher is the party that publishes information by defining one or more topic and then sending messages to those topic.
Publisher's constructor is as follows: Java code public publisher () throws jmsexception { factory = new Activemqconnectionfactory (brokerurl); connection = factory.createconnection (); try { connection.start (); } catch (JMSEXCEPTION JMSE) { connection.close (); throw jmse; } session = Connection.createsession (false, sessIon. Auto_acknowledge); producer = Session.createproducer (null); }
We define the basic ConnectionFactory, connection, session, producer according to the process described above. Here the code is the main implementation of initialization effect.
Next, we need to define a series of topic let all the consumer to subscribe, set topic code as follows: Java code protected void Settopics (string[] stocks) throws JMSException { Destinations = new Destination[stocks.length]; for (int i = 0; i < stocks.length; i++) {Destinations[i] = session.createtopic ("stocks." + stocks[i]); } }
Here destinations is an internally defined member variable destination[]. The total number of topic we have defined here depends on the given parameter stocks.
After defining the topic, we will send messages to these specified topic, with the following code: Java code protected void SendMessage (string[] stocks) throws JMSException { for ( int i = 0; i < stocks.length; i++) { message message = createstockmessage (stocks[i], session); system.out.println ("sending: " + ( activemqmapmessage) message). Getcontentmap () + " on destination: " + Destinations[i]); producer.send (Destinations[i], message); } } protected Message createstockmessage (string stock, session session) throws JMSException { &nbsP; mapmessage message = session.createmapmessage (); message.setstring ("Stock", stock); message.setdouble ("Price", 1.00); message.setdouble ("offer", 0.01); message.setboolean ("Up", true); return message; }
The preceding code is simple, in the SendMessage method we iterate through each topic and send a defined message message to each topic.
Once we've defined the basics of sending messages before, it's easy to call their code: Java Code public static void main (string[] args) throws JMSException { if (args.length < 1) throw new IllegalArgumentException (); // Create publisher publisher publisher = new publisher (); // set topics publisher.settopics (args); for (int i = 0; i < 10; i++) &NBSP;{&NBSP;&NBsp publisher.sendmessage (args); system.out.println ("publisher " + i + " price" messages "); try { thread.sleep (; ) } catch (interruptedexception e) { e.printstacktrace (); } } // Close all resources publisher.close (); }
Calling their code is that we traverse all topic and send messages through SendMessage. Sleep1 seconds after a message is sent. One of the areas to be aware of is that we are using resources