In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
Consumer 1 (Consumer)-- subscription (subcribe)-- > topic (Topic)
Package com.java1234.activemq2;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageConsumer;import javax.jms.Session;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;/** * message consumer-message subscriber 1 * @ author Administrator * * / public class JMSConsumer {private static final String USERNAME=ActiveMQConnection.DEFAULT_USER / / default connection username private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; / / default connection password private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; / / default connection address public static void main (String [] args) {ConnectionFactory connectionFactory; / / connection factory Connection connection = null / / connection Session session; / / Thread that accepts or sends messages Destination destination; / / destination of messages MessageConsumer messageConsumer; / / Consumer of messages / / instantiate connection factory connectionFactory=new ActiveMQConnectionFactory (JMSConsumer.USERNAME, JMSConsumer.PASSWORD, JMSConsumer.BROKEURL) Try {connection=connectionFactory.createConnection (); / / obtain the connection connection.start () through the connection factory; / / start the connection session=connection.createSession (Boolean.FALSE, Session.AUTO_ACKNOWLEDGE) / / create Session / / destination=session.createQueue ("FirstQueue1"); / / create a connected message queue destination=session.createTopic ("FirstTopic1"); messageConsumer=session.createConsumer (destination); / / create a message consumer messageConsumer.setMessageListener (new Listener ()) / / Registration message listening} catch (JMSException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}} package com.java1234.activemq2;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage / * message monitoring-subscriber 1 * @ author Administrator * * / public class Listener implements MessageListener {@ Override public void onMessage (Message message) {/ / TODO Auto-generated method stub try {System.out.println ("message received by subscriber:" + (TextMessage) message) .getText ()) } catch (JMSException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
Consumer 2 (Consumer)-- subscription (subcribe)-- > topic (Topic)
Producer (Producer)-publish (publish)-> topic (Topic)
Package com.java1234.activemq2;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.JMSException;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;/** * message producer-message publisher * @ author Administrator * * / public class JMSProducer {private static final String USERNAME=ActiveMQConnection.DEFAULT_USER / / default connection username private static final String PASSWORD=ActiveMQConnection.DEFAULT_PASSWORD; / / default connection password private static final String BROKEURL=ActiveMQConnection.DEFAULT_BROKER_URL; / / default connection address private static final int SENDNUM=10; / / number of messages sent public static void main (String [] args) {ConnectionFactory connectionFactory / / connection factory Connection connection = null; / / connection Session session; / / the thread that accepts or sends the message Destination destination; / / the destination MessageProducer messageProducer of the message / / message producer / / instantiate connection factory connectionFactory=new ActiveMQConnectionFactory (JMSProducer.USERNAME, JMSProducer.PASSWORD, JMSProducer.BROKEURL); try {connection=connectionFactory.createConnection (); / / obtain connection connection.start through connection factory () / / start connection session=connection.createSession (Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); / / create Session / / destination=session.createQueue ("FirstQueue1"); / / create message queuing destination=session.createTopic ("FirstTopic1"); / / create topic messageProducer=session.createProducer (destination) / / create message producer sendMessage (session, messageProducer); / / send message session.commit ();} catch (Exception e) {/ / TODO Auto-generated catch block e.printStackTrace () } finally {if (connectiondiagnosis null) {try {connection.close () } catch (JMSException e) {/ / TODO Auto-generated catch block e.printStackTrace () }} / * send a message * @ param session * @ param messageProducer * @ throws Exception * / public static void sendMessage (Session session,MessageProducer messageProducer) throws Exception {for (int iTuno * I)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.