The role of this pattern is that publishers and subscribers can send messages to each other
Both Publishers and Subscribers act as producers and consumers
Published by
Package publisher.to.subscriber;
Import Java.awt.font.TextMeasurer;
Import javax.jms.Connection;
Import javax.jms.Destination;
Import javax.jms.JMSException;
Import Javax.jms.MapMessage;
Import Javax.jms.Message;
Import Javax.jms.MessageConsumer;
Import Javax.jms.MessageListener;
Import Javax.jms.MessageProducer;
Import javax.jms.Session;
Import Javax.jms.TextMessage;
Import Org.apache.activemq.ActiveMQConnectionFactory;
Import Org.apache.activemq.command.ActiveMQTopic;
public class Publisher implements MessageListener {
/**
* @param args
* @throws jmsexception
*/
public static void Main (string[] args) throws JMSException {
TODO auto-generated Method Stub
Activemqconnectionfactory factory = new Activemqconnectionfactory ();
Connection Connection = Factory.createconnection ();
Connection.start ();
First parameter setting whether transaction support is required
Session session = Connection.createsession (True,
Session.auto_acknowledge);
Send a message and the Subscriber accepts the message must have a topic name also called Msg.send
Destination sendtopic = new Activemqtopic ("Msg.message");
Accepts a message and the Subscriber sends a message must have a topic name, also called Msg.receive
Destination sendreceive = new Activemqtopic ("Msg.control");
MessageProducer producer = Session.createproducer (sendtopic);
Messageconsumer consumer = Session.createconsumer (sendreceive);
REC rec = new Rec (consumer,session,connection,producer);
Rec.start ();
}
public void OnMessage (Message msg) {
TODO auto-generated Method Stub
}
}
Class Rec extends Thread {
private Messageconsumer consumer = null;
Private session session = NULL;
Private Connection Connection=null;
MessageProducer Producer=null;
Public Rec (Messageconsumer consumer, Session session,connection connection,messageproducer producer) {
This.consumer = consumer;
This.session = session;
This.connection=connection;
This.producer=producer;
}
@Override
public void Run () {
TODO auto-generated Method Stub
while (true) {
try {
TextMessage smsg = session.createtextmessage ("Hello my name is Liaomin");
Producer.send (smsg);
Session.commit ();
TextMessage msg = (textmessage) consumer.receive ();
System.out.println (Msg.gettext ());
You must submit the following when you receive the old data that was last received will appear
Session.commit ();
Session.close ();
Connection.close ();
Break
} catch (Exception e) {
}
}
}
}
Subscribed by
Package publisher.to.subscriber;
Import javax.jms.Connection;
Import javax.jms.Destination;
Import javax.jms.JMSException;
Import Javax.jms.MapMessage;
Import Javax.jms.Message;
Import Javax.jms.MessageConsumer;
Import Javax.jms.MessageProducer;
Import javax.jms.Session;
Import Javax.jms.TextMessage;
Import Org.apache.activemq.ActiveMQConnectionFactory;
Import Org.apache.activemq.command.ActiveMQTopic;
public class Subscriber {
/**
* @param args
* @throws jmsexception
*/
public static void Main (string[] args) throws JMSException {
TODO auto-generated Method Stub
Activemqconnectionfactory factory = new Activemqconnectionfactory ();
Connection Connection = Factory.createconnection ();
Connection.start ();
First parameter setting whether transaction support is required
Session session = Connection.createsession (True,
Session.auto_acknowledge);
Subscribers Accept Messages
Destination receivetopic = new Activemqtopic ("Msg.message");
Subscribers send messages
Destination sendtopic = new Activemqtopic ("Msg.control");
MessageProducer producer = Session.createproducer (sendtopic);
Messageconsumer consumer = Session.createconsumer (receivetopic);
REC1 rec = new REC1 (consumer,session,connection,producer);
Rec.start ();
}
public void OnMessage (Message msg) {
TODO auto-generated Method Stub
}
}
Class Rec1 extends Thread {
private Messageconsumer consumer = null;
Private session session = NULL;
Private Connection Connection;
Private MessageProducer producer;
Public Rec1 (Messageconsumer consumer, Session session,connection connection,messageproducer producer) {
This.consumer = consumer;
This.session = session;
This.connection=connection;
This.producer=producer;
}
@Override
public void Run () {
TODO auto-generated Method Stub
while (true) {
try {
TextMessage msg = (textmessage) consumer.receive ();
Session.commit ();
System.out.println (Msg.gettext ());
TextMessage remsg = session.createtextmessage ("OK receive");
Producer.send (REMSG);
You must submit the following when you receive the old data that was last received will appear
Session.commit ();
Session.close ();
Connection.close ();
Break
} catch (Exception e) {
}
}
}
}
Run subscribers before running the publisher
Publisher/subscriber (publisher/subscriber) message pattern development process