In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about the introduction to ActiveMQ, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
1. Publish a message
Import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.*;public class BookProducer implements Runnable {public static final String BROKER_URL = "tcp://localhost:61616"; @ Override public void run () {try {/ / 1. Create a connection factory and specify ip and port ConnectionFactory factory = new ActiveMQConnectionFactory (BROKER_URL); / / 2. Use the connection factory to create a connection object Connection connection = factory.createConnection (); / / 3. Open a connection (JMS session) connection.start (); Session session = connection.createSession (false, Session.AUTO_ACKNOWLEDGE); / / create a destination using the session / * ① point-to-point (Point-to-Point). In a peer-to-peer messaging system, messages are distributed to a single consumer. Peer-to-peer messages are often associated with queues (javax.jms.Queue). * ② publish / subscribe (Publish/Subscribe). Publish / subscribe messaging system supports an event-driven model in which both message producers and consumers participate in message delivery. The producer publishes the event, while the consumer subscribes to the event of interest and uses it. This type of message is generally associated with a specific topic (javax.jms.Topic). * / Destination destination = session.createQueue ("book-broker") / / create producer / consumer MessageProducer producer = session.createProducer (destination); / / MessageConsumer consumer = session.createConsumer (destination); / / consumer.receive () / * create messages. Supported message types: * TextMessage * MapMessage * ObjectMessage: object needs to implement serialization API * BytesMessage * StreamMessage * / Message message = session.createTextMessage ("I am a banana.") / / send message producer.send (message); / / release resource producer.close (); session.close (); connection.close ();} catch (JMSException e) {e.printStackTrace ();}
two。 Receive messages
Import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.JMSException;import javax.jms.Session;import javax.jms.TextMessage;public class BookConsumer implements Runnable {@ Override public void run () {try {var connection = new ActiveMQConnectionFactory (BookProducer.BROKER_URL). CreateConnection (); connection.start (); / * * connection.createSession (boolean transacted, int acknowledgeMode) * transacted: whether to use transaction * acknowledgeMode: reply mode * AUTO_ACKNOWLEDGE: automatic reply * for synchronous consumers, the receive method call returns, and when no exception occurs, the received message will be automatically confirmed. * for asynchronous messages, the received message is automatically acknowledged when the onMessage method returns and no exception occurs. * CLIENT_ACKNOWLEDGE: the client responds manually * this method requires the client to use the javax.jms.Message.acknowledge () method to complete the confirmation. * DUPS_OK_ACKNOWLEDGE: delay / / batch notification * this confirmation method allows JMS not to rush to acknowledge received messages and to complete acknowledgements at once after receiving multiple messages, which may be more effective in some cases than Auto_AcKnowledge because there is no acknowledgment. When the system crashes or the network fails, the message can be retransmitted. * use transaction message acknowledgement mode: * SESSION_TRANSACTED * / var session = connection.createSession (false, Session.AUTO_ACKNOWLEDGE); var consumer = session.createConsumer (session.createQueue ("tmall-queue")); var message = ((TextMessage) consumer.receive ()). GetText (); System.out.println (message) Session.close (); connection.close ();}}
Or set the listener to receive (consumers do not have to be online all the time, listen to receive messages automatically)
Import org.apache.activemq.ActiveMQConnectionFactory;import javax.jms.JMSException;import javax.jms.Session;import javax.jms.TextMessage;public class BookConsumer implements Runnable {@ Override public void run () {try {var connection = new ActiveMQConnectionFactory (BookProducer.BROKER_URL) .createConnection (); connection.start (); var session = connection.createSession (false, Session.AUTO_ACKNOWLEDGE) Var consumer = session.createConsumer (session.createQueue ("tmall-queue")); consumer.setMessageListener (message-> {try {System.out.println (TextMessage) message). GetText ());} catch (JMSException e) {e.printStackTrace ();}}) } catch (JMSException e) {e.printStackTrace ();}
3. Java embeds ActiveMQ to automatically start an ActiveMQ without the need for Linux startup
Import org.apache.activemq.broker.BrokerService;public class Broker {/ / Import dependency compile group: 'com.fasterxml.jackson.core', name:' jackson-core', version: '2.9.9' public void producter () {BrokerService brokerService = new BrokerService (); brokerService.setUseJmx (true); / / sets whether Broker's service should be exposed to JMX try {brokerService.addConnector ("tcp://localhost:61616") BrokerService.start ();} catch (Exception e) {e.printStackTrace ();} above is the introduction to ActiveMQ. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.