ActiveMq usage and activemq usage
I. Introduction to activeMq
ActiveMQ is an open-source middleware that implements the JMS1.1 specification and is oriented to message (MOM, provides efficient, scalable, stable, and secure enterprise-level message communication for applications.
<bean id="jettyPort" class="org.apache.activemq.web.WebConsolePort" init-method="start"> <!-- the default port number for the web console --> <property name="host" value="0.0.0.0"/> <property name="port" value="8161"/> </bean>
Ii. activeMq usage
ActiveMq uses java multi-threaded classic producers and consumers for processing.
1: Create a producer first
// Connection factory jms use it to create connection connectionFactory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_PASSWORD, ActiveMQConnection. DEFAULT_BROKER_URL); // jms Connection connection = connectionFactory. createConnection (); // Session session = connection. createSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE); // Message Destination, to which the message is sent, destination Destination = session. createQueue ("threadMsg"); // obtain the producer's target image MessageProducer messageProducer = session. createProducer (destination); // sets the message object TextMessage message = session. createTextMessage ("messages sent by ActiveMq" + index); // send producer. send (message );
After sending consumption, You can query it in the activeMq background.
Click threadMsg to view the list of unread messages and view the details of unread messages.
2: create a message consumer
// The Connection factory jms uses it to create a connection ConnectionFactory connectionFactory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_PASSWORD, ActiveMQConnection. DEFAULT_BROKER_URL); // jms Connection connection = connectionFactory. createConnection (); // create an operation to connect to Session session = connection. createSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE); // gets the operation connection threadMsg and needs to send messages to the producer The value corresponds to Destination destination = session. createQueue ("threadMsg"); // message receiving MessageConsumer = messageConsumersession. createConsumer (destination); while (true) {// reads a message once per second // sets the time when the receiver receives the message. To facilitate testing, who is set to 100 s TextMessage message = (TextMessage) messageConsumer. receive (100000); if (null! = Message) {log.info (threadName + "Receive message" + message. getText () ;}else {// break ;} Thread. sleep (1000 );}
Note: Because maven is not used in this project, the environment may be different, and logg4j and other packages cannot be found.
Code: github