Publisher/subscriber (publisher/subscriber) message pattern development process

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.