ActiveMQ two message modes

1. Peer-to-peer mode (p2p) 2. Example



of publish/subscribe mode (pub/sub) :





public class Producer {
	// Create a connectionFactory factory object
	private ActiveMQConnectionFactory connectionFactory;
	// connection object
	private Connection connection;
	// session object
	private Session session;
	// producer
	private MessageProducer producer;

	public Producer() {
		this.connectionFactory = new ActiveMQConnectionFactory();
		try {
			this.connection = connectionFactory.createConnection("fu", "fu");
			this.connection.start();
			//Refer to one: The transaction is not opened, refer to the second, automatically sign for receipt
			this.session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			this.producer = session.createProducer(null);

		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	public void send() throws Exception {
		//Create publish/subscribe message mode
		Destination destination = this.session.createTopic("topic1");
		Message m = this.session.createTextMessage("a message");
		this.producer.send(destination, m);

        //close the connection
        this.connection.close();
	}

	public static void main(String[] args) throws Exception {
		Producer p = new Producer();
		p.send();
	}

}



public class Comsumer {


	// Create a connectionFactory factory object
	private ActiveMQConnectionFactory connectionFactory;
	// connection object
	private Connection connection;
	// session object
	private Session session;
	// producer
	private MessageConsumer consumer;
	// target address
	private Destination destination;

	public Comsumer() {
		this.connectionFactory = new ActiveMQConnectionFactory();
		try {
			this.connection = connectionFactory.createConnection("fu", "fu");
			this.connection.start();
			//Refer to one: The transaction is not opened, refer to the second, automatically sign for receipt
			this.session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
			//Create publish/subscribe message mode
			this.destination = this.session.createTopic("topic1");
			// The message is not filtered by the message itself, but by some attributes attached to the filter
			this.consumer = session.createConsumer(destination);
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}

	public void recever() throws Exception {
		// Asynchronous reception of messages: When a message arrives, ActiveMQ notifies the consumer actively, you can register a MessageListener class to implement the onMessage method to monitor the MQ delivery of messages
		this.consumer.setMessageListener(new Listener());

	}

	class Listener implements MessageListener {

		public void onMessage(Message message) {
			try {
				TextMessage m = (TextMessage) message;
				System.out.println(m.getText()+"===============");
			} catch (JMSException e) {
				e.printStackTrace ();
			}
		}

	}

	public static void main(String[] args) throws Exception {
		Comsumer c = new Comsumer();
		c.recever();
	}
}



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326324248&siteId=291194637