Download the HORNETQ and unpack, start the Run.bat hornetq service below the bin directory.
The "Springintegration" in the demo implements the asynchronous pattern in a listener way.
In the demo, the "topic" C-end obtains the message through the receive () method, realizes the synchronization mode.
One, the following according to the examples of these two models to do the configuration instructions:
(1) topic (Sync):
Step1. Create an initial context execution directory
InitialContext = GetContext (0)
Step2. Topic to execute a query
Topic Topic = (Topic) initialcontext.lookup ("/topic/mesdemotopic");
"/topic/mesdemotopic": to be as jndi as the server configuration.
Step3. Execute a Query connection factory
ConnectionFactory CF = (connectionfactory) initialcontext.lookup ("/connectionfactory");
Step4. Create a connection and session
Connection = Cf.createconnection ();
Session session = Connection.createsession (false, Session.auto_acknowledge);
The first parameter is whether the transaction is used, and the second parameter is how the consumer confirms to the sender that the message has been received.
There are three ways to confirm messages:
Auto_acknowledge (automatic notification)
Client_acknowledge (client's own decision to notify time)
Dups_ok_acknowledge (Delay//batch notification)
Step5. Create a sender of a message producer
MessageProducer producer = session.createproducer (topic);
Step 6. Create a consumer
Messageconsumer messageConsumer1 = session.createconsumer (topic);
STEP7. Create a message
TextMessage message = Session.createtextmessage (' This is a-text message ');
The message also includes:
(1) Byte array type: Bytesmessage
(2) Map type: Mapmessage
(3) Multiple raw data types: streammessage
(4) Object type: ObjectMessage
Step8. Send the message
Producer.send (message);
Step9. Receive the message
TextMessage messagereceived = (textmessage) messageconsumer1.receive ();
Step10. Close related connections and services
if (connection!= null)
{
Connection.close ();
}
if (InitialContext!= null)
{
Initialcontext.close ();
}
(2) springintegration (asynchronous) mode:
First of all, this example is integrated with spring.
Step1. To create a bean factory from a configuration file
Classpathxmlapplicationcontext context = new Classpathxmlapplicationcontext (new string[] {"Spring-jms-beans.xml"}); (Please see demo for specific configuration file)
Step2. Get message Sender Messagesender
Messagesender sender = (messagesender) context.getbean ("Messagesender");
Step3. Send a message
Sender.send ("Hello World");
This method creates the connection as well as session and producer, as is the case with topic.
STEP4.C received the message at the end.
When the P-side sends a message to broker, the MessageListener is taken to the queue or topic message when the listening queue or topic channel in the C-terminal changes.