Basic configuration and example of ActiveMQ: basic configuration of activemq

Source: Internet
Author: User

Basic configuration and example of ActiveMQ: basic configuration of activemq
1. Download ActiveMQ

Go to the official website download: http://activemq.apache.org/

Ii. Run ActiveMQ

Decompress apache-activemq-5.11.1-bin.zip. Because the system is 32-bit, enter the apache-activemq-5.11.1 \ bin \ win32 Directory.

1. Install InstallService. bat. If so, you may have installed the service on your computer.

/** To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package testactivemq; import javax. jms. *; import org. apache. activemq. activeMQConnection; import org. apache. activemq. activeMQConnectionFactory;/***** @ author lin np */public class JmsSender {private ConnectionFactory connect IonFactory = null; private Connection connection = null; private Session session = null; private Destination destination = null; private MessageProducer producer = null; private static final int SEND_NUMBER = 5; /*****/public void init () {// construct the ConnectionFactory instance object. The implementation of ActiveMq jar connectionFactory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_P ASSWORD, "tcp: // localhost: 61616"); // ActiveMQ uses the default TCP connection port 61616 try {// construct the connection object connection = connectionFactory from the factory. createConnection (); connection. start (); // get the operation connection session = connection. createSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE);/*** first method: Queue * // destination = session. createQueue ("xkey"); // "xkey" can be used to obtain other values. // Producer = session. createProducer (destination); // obtain the message generator [Sender]/*** second method: Topic */Topic topic = session. createTopic ("xkey. topic "); producer = session. createProducer (topic);/***** // The settings are not persistent. Learn here and decide the producer according to the project. setDeliveryMode (DeliveryMode. NON_PERSISTENT); // construct a message. The message is written to an end here. The project is a parameter, or the method used to obtain sendMessage (session, producer); session. commit ();} catch (Exception e) {e. printStackTrace ();} finally {try {connection. close ();} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace () ;}} private void sendMessage (Session session, MessageProducer producer) throws JMSException {for (int I = 1; I <= SEND_NUMBER; I ++) {TextMessage message = session. createTextMessage ("ActiveMq message:" + I); // send the message System. out. println ("Send message:" + "ActiveMq message:" + I); producer. send (message) ;}}/*** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub JmsSender jms = new JmsSender (); jms. init ();}}

JmsReceiver. java File

/** To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */package testactivemq; import javax. jms. *; import org. apache. activemq. activeMQConnection; import org. apache. activemq. activeMQConnectionFactory;/***** @ author lin np */public class JmsReceiver {private ConnectionFactory conne CtionFactory = null; private Connection connection = null; private Session session = null; private MessageConsumer consumer = null; private Destination destination = null; public void init () {connectionFactory = new ActiveMQConnectionFactory (ActiveMQConnection. DEFAULT_USER, ActiveMQConnection. DEFAULT_PASSWORD, "tcp: // localhost: 61616"); // The default TCP connection port used by ActiveMQ is 61616 try {// construct the connection object obtained from the factory = ConnectionFactory. createConnection (); connection. start (); // get the operation connection session = connection. createSession (Boolean. TRUE, Session. AUTO_ACKNOWLEDGE);/*** first method: Queue * // destination = session. createQueue ("xkey"); // consumer = session. createConsumer (destination);/*** Method 2: Topic */Topic topic = session. createTopic ("xkey. topic "); consumer = session. createConsumer (topic);/*****/while (true ){/ /Set the time for the receiver to receive the message. For the convenience of testing, who is set to 500 s TextMessage message = (TextMessage) consumer. receive (500000); if (null! = Message) {System. out. println ("Receiver" + message. getText () ;}else {break ;}} catch (Exception e) {e. printStackTrace ();} finally {try {connection. close ();} catch (JMSException e) {// TODO Auto-generated catch block e. printStackTrace () ;}}/ *** @ param args */public static void main (String [] args) {// TODO Auto-generated method stub JmsReceiver jms = new JmsReceiver (); jms. init ();}}
4. Test process

The activemq. bat service is running throughout the process.

4.1 run the JmsReceiver. java file. The testActiveMQ (run) console displays

Log4j: WARN No appenders cocould be found for logger (org. apache. activemq. transport. WireFormatNegotiator ).
Log4j: WARN Please initialize the log4j system properly.
Log4j: WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

4.2 run the JmsSender. java file, testActiveMQ (run) #2 console will appear

Log4j: WARN No appenders cocould be found for logger (org. apache. activemq. transport. WireFormatNegotiator ).
Log4j: WARN Please initialize the log4j system properly.
Log4j: WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Send message: the message sent by ActiveMq: 1
Send message: the message sent by ActiveMq: 2
Send message: the message sent by ActiveMq: 3
Send message: the message sent by ActiveMq: 4
Send message: the message sent by ActiveMq: 5

The warning information can be ignored, and you have not found the source of the warning.

4.3 returned to the testActiveMQ (run) Console

Run:
Log4j: WARN No appenders cocould be found for logger (org. apache. activemq. transport. WireFormatNegotiator ).
Log4j: WARN Please initialize the log4j system properly.
Log4j: WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
The message sent by the Receiver ActiveMq: 1
Messages sent by the Receiver ActiveMq: 2
Messages sent by the Receiver ActiveMq: 3
The message sent by the Receiver ActiveMq: 4
Messages sent by the Receiver ActiveMq: 5

 

Main reference:

Http://www.cnblogs.com/xwdreamer/archive/2012/02/21/2360818.html

Http://www.open-open.com/lib/view/open1388994166156.html

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.