ActiveMQ persistence (file), query queue remaining message number, the implementation of team number

Source: Internet
Author: User
Tags message queue

"ACTIVEMQ send message and receive message" in detail ACTIVEMQ send messages and receive messages, messages stored in Message Queuing (queue), message queue data stored in computer memory, if the ACTIVEMQ server for some reason suddenly stopped, the message queue content is still there. Talk to the truth, stop the ACTIVEMQ server, and then look at the queue information queues on the Activemq page, as shown:


All the information in the Activemq_queue queue is missing. To solve this problem, the queue data can be persisted, including file persistence and database persistence.

Now analyze file persistence in detail. Come directly to the code:

Message sender:

Package com.activemq.queue; Import javax.jms.Connection; public class Sender {private static final int send_number = 1; public static void Main (string[] args) {//Connectionfact Ory Interface (connection factory) A pipe object that a user uses to create a connection to a JMS provider. JMS clients access the connection through a portable interface so that the code does not need to be modified when the implementation of the current layer changes. Administrators configure connection factories in the Jndi namespace so that JMS clients can find them. Depending on the type of message, the user will use the queue to connect to the factory or the subject to connect to the factory. ConnectionFactory ConnectionFactory; The CONNECTION:JMS client to the JMS Provider connection//Connection interface (connection) connection represents the communication link between the application and the messaging server. Once you have the connection factory, you can create a connection to the JMS provider. Depending on the type of connection,//connection allows the user to create a session to send and receive queues and topics to the destination. Connection Connection = null; Sessions: a thread/session interface that sends or receives messages represents a single-threaded context for sending and receiving messages. Because the session is single-threaded, the message is sequential, that is, the message is received in the order sent. The benefit of a session is that it supports transactions. If the user chooses transaction support, the session context saves a set of messages until the transaction is committed. The user can use a rollback operation to cancel these messages before committing the transaction. A session allows a user to create a message producer to send a message, creating a message consumer to receive the message. Session session; Destination: The destination of the message, and to whom the message is sent. The destination interface (target) target is a Tube object that wraps the message target identifier,//message target refers to the location where the message was published and received, or the queue, or the subject. The JMS administrator creates these objects and then the user discovers them through Jndi. As with connection factories, administrators can create two types of goals, a point-to-point model queue, and a publisher/Subscriber model theme. DestInation destination; MessageProducer: Message sender//messageproducer interface (message producer) An object created by a session to send a message to a destination. Users can create a sender of a target, or they can create a generic sender, specifying a target when sending a message. MessageProducer producer; TextMessage message; Constructs a ConnectionFactory instance object, where the ACTIVEMQ implementation jar ConnectionFactory = new Activemqconnectionfactory ( Activemqconnection.default_user,//null Activemqconnection.default_password,//null "tcp://localhost:61616"); try {//construct gets connection object from factory connection = Connectionfactory.createconnection ();//start Connection.start ();//Get Action Connection session = Conn Ection.createsession (Boolean.true, Session.auto_acknowledge); 1 destination = Session.createqueue (runserver.queuename); Get message Creator "Sender" producer = Session.createproducer (destination); setting is not persisted, you can change Producer.setdeliverymode (deliverymode.persistent); 2//Construction message SendMessage (session, producer); Session.commit (); catch (Exception e) {e.printstacktrace ();} finally {try {if (null!= connection) connection.close ();} catch (Throwa ble ignore) {}}} public static void SendmesSage (Session sessions, MessageProducer producer) throws Exception {for (int i = 1; I <= send_number; i++) {TextMessage message = Session. Createtextmessage ("Messages sent by ActiveMq" + i); Send message to Destination place System.out.println ("Send message:" + i); Producer.send (message); } } }

Message recipient:

Package com.activemq.queue; Import javax.jms.Connection; public class Receiver {public static void main (string[] args) {//ConnectionFactory: Connection factory, JMS use it to create connection ConnectionFactory C Onnectionfactory; CONNECTION:JMS Client to JMS Provider connection Connection Connection = null; Session: A thread that sends or receives messages final session session; Destination: The destination of the message, and to whom the message is sent. Destination destination; Consumer, message receiver//Messageconsumer interface (message consumer) an object created by a session to receive messages sent to a destination. Consumers can synchronize (blocking mode), or receive messages of queue and subject type asynchronously (non-blocking). Messageconsumer consumer; ConnectionFactory = new Activemqconnectionfactory (Activemqconnection.default_user, Activemqconnection.default_ PASSWORD, "tcp://localhost:61616"); try {//construct gets connection object from factory connection = Connectionfactory.createconnection ();//start Connection.start ();//Get Action Connection session = Conn Ection.createsession (Boolean.true,session.auto_acknowledge); Queue_name is consistent with sender, one creates one to receive Destination = Session.createqueue (runserver.queuename); Consumer = session.createconsumer (destination); The first case//int i = 0; WHile (I < 3) {//i++;//TextMessage message = (textmessage) consumer.receive ();//Session.commit ();<

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.