First, the ad-hoc mode:
There are provided, and the recipient
QueueProductor
public static void main (String [] args) throws a JMSException { // . 1, create the connection factory ActiveMQConnectionFactory The connectionFactory = new new ActiveMQConnectionFactory ( "TCP: //192.168.200.128: 61616" ); // 2, Get Link Connection Connection = The connectionFactory. the createConnection (); // . 3, initiates the connection connection.start (); // . 4, whether to start acquiring a transaction session parameter, parameter 2, message acknowledgment mode the session session = connection.createSession ( to false , Session.AUTO_ACKNOWLEDGE); // 5, create a queue objects = Session.createQueue Queue Queue ( "the Test-queque" ); // 6, create the message producers MessageProducer Producer = session.createProducer (Queue); // 7, create a message TextMessage textMessage = session.createTextMessage ( "Welcome to the XX Institute " ); // 8, sends a message producer.send (textMessage); // . 9, close the resource producer.close (); Session.close (); Connection.close (); }
QueueConsumer
public static void main (String [] args) throws a JMSException, IOException { // . 1, create the connection factory ActiveMQConnectionFactory The connectionFactory = new new ActiveMQConnectionFactory ( "TCP: //192.168.200.128: 61616" ); // 2, Get Link Connection connection = connectionFactory.createConnection (); // . 3, initiates the connection connection.start (); // . 4, whether to start acquiring a transaction session parameter, parameter 2, message acknowledgment mode the session session = connection.createSession ( to false , Session.AUTO_ACKNOWLEDGE); // 5 to create the queue object = Session.createQueue Queue Queue ( "Test-queque" ); // . 6, create message producers a MessageConsumer Consumer = session.createConsumer (Queue); // . 7, listening for messages consumer.setMessageListener ( new new the MessageListener () { @Override public void the onMessage (the message message) { TextMessage textMessage = (TextMessage) message; the try { System.out.println ( "currently received message:" + textMessage.getText ()); } the catch(A JMSException E) { e.printStackTrace (); } } }); // . 8, waiting for keyboard input System.in.read (); // . 9, close the resource consumer.close (); Session.close (); Connection .close (); }
Second, publish and subscribe
A provider, multiple consumers
TopicProducer
public static void main(String[] args) throws JMSException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.200.128:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic("test-topic"); MessageProducer producer = session.createProducer(topic); TextMessage textMessage = session.createTextMessage("消息队列广播"); producer.send(textMessage); producer.close(); session.close(); connection.close(); }
TopicConsumer
public static void main(String[] args) throws JMSException, IOException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.200.128:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic("test-topic"); MessageConsumer consumer = session.createConsumer(topic); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("2接收到消息"+textMessage.getText()); }catch (JMSException e){ e.printStackTrace(); } } }); System.in.read(); consumer.close(); session.close(); connection.close(); }
TopicConsumer2
public static void main(String[] args) throws JMSException, IOException { ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.200.128:61616"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic("test-topic"); MessageConsumer consumer = session.createConsumer(topic); consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { TextMessage textMessage = (TextMessage) message; try { System.out.println("1接收到消息"+textMessage.getText()); }catch (JMSException e){ e.printStackTrace(); } } }); System.in.read(); consumer.close(); session.close(); connection.close(); }
note:
Point to point, providers and consumers who should open it does not matter, but in a message publish and subscribe, consumers must start before the provider, startup is completed, and listens. Obtain data message queue