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--
Producer (producer)-> message queue (message queue)
Package com.java1234.activemq;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 * @ 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 queue messageProducer=session.createProducer (destination); / / create message producer sendMessage (session, messageProducer) / / send the 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 message * @ param session * @ param messageProducer * @ throws Exception * / public static void sendMessage (Session session,MessageProducer messageProducer) throws Exception {for (int iTun0) I message queue
Package com.java1234.activemq;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 javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;/** * message consumer * @ author Administrator * * / public class JMSConsumer2 {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 (JMSConsumer2.USERNAME, JMSConsumer2.PASSWORD, JMSConsumer2.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 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 ();}
Listener message queuing
Package com.java1234.activemq;import javax.jms.JMSException;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage / * message snooping * @ author Administrator * * / public class Listener implements MessageListener {@ Override public void onMessage (Message message) {/ / TODO Auto-generated method stub try {System.out.println ("received message:" (TextMessage) message) .getText ()) } catch (JMSException e) {/ / TODO Auto-generated catch block e.printStackTrace ();}}
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.