ActiveMQ Getting Started Example

1. Download ActiveMQ

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

2. Run ActiveMQ

Unzip apache-activemq-5.5.1-bin.zip, then double-click apache-activemq-5.5.1\bin\activemq.bat to run the ActiveMQ program.

After starting ActiveMQ, log in: http://localhost:8161/admin/ , create a Queue and name it FirstQueue.

3. Create an Eclipse project and run

Create project: ActiveMQ-5.5, and import the required jar files in the apache-activemq-5.5.1\lib directory. The project structure is shown in the following figure:

3.1.Sender.java

copy code
package com.xuwei.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {
private static final int SEND_NUMBER = 5;

public static void main(String[] args) {
// ConnectionFactory : The connection factory that JMS uses to create connections
ConnectionFactory connectionFactory;
// Connection : The connection from the JMS client to the JMS Provider
Connection connection = null ;
// Session: A thread that sends or receives messages
Session session;
// Destination : The destination of the message; to whom the message is sent.
Destination destination;
// MessageProducer: message sender
MessageProducer producer;
// TextMessage message;
// Construct ConnectionFactory instance object, where ActiveMq implementation is used jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER ,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// Construct the connection object obtained from the factory
connection = connectionFactory.createConnection();
// Start
connection.start();
// Get the operation connection
session = connection .createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// Get the session attention parameter value xingbo.xu-queue is a server queue, you must configure destination = session.createQueue("FirstQueue") in ActiveMq's console ; // Get the message producer [sender] producer = session.createProducer(destination); // The setting is not persistent, learn here, it is actually decided according to the project




producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// Construct the message, write it here, the item is the parameter, or the method gets
sendMessage(session, producer);
session.commit();
} catch (Exception e) {
e.printStackTrace( );
} finally {
try {
if ( null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}

public static void sendMessage(Session session, MessageProducer producer)
throwsException {
for ( int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session
.createTextMessage("Message sent by ActiveMq" + i);
// Send message to destination
System.out.println("Send message : " + "Message sent by ActiveMq" + i);
producer.send(message);
}
}
}
copy code

3.2.Receiver.java

copy code
package com.xuwei.activemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory;
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
consumer = session.createConsumer(destination);
while (true) {
//设置接收者接收消息的时间,为了便于测试,这里谁定为100s
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != connection)
connection.close();
} catch (Throwable ignore) {
}
}
}
}
copy code

4.注意事项

  1. 最后接收者跟发送者在不同的机器上测试
  2. 项目所引用的jar最后在ActiveMQ下的lib中找,这样不会出现版本冲突。

5.测试过程

因为是在单机上测试,所以需要开启两个eclipse,每一个eclipse都有自身的workspace。我们在eclipse1中运行Receiver,在eclipse2中运行Sender。

刚开始eclipse1中运行Receiver以后console介面没有任何信息,在eclipse2中运行Sender以后,eclipse2中的console显示如下信息:

Send message: ActiveMq sent message 1
Send message: ActiveMq sent message 2
Send message: ActiveMq sent message 3
Send message: ActiveMq sent message 4
Send message: ActiveMq sent message 5

When I returned to eclipse1, I found the following information on the console interface:

Received message ActiveMq sent message 1
Received message ActiveMq sent message 2
Received message ActiveMq sent message 3
Received message ActiveMq sent message 4
Received message ActiveMq sent message 5

 PS:2012-2-27

Today, I found that the test does not need to open two eclipses. You can start multiple programs on the next page of an eclipse, and there are multiple consoles. In the above Receiver.java, set a larger time, such as receive(500000), as follows The code shows:

TextMessage message = (TextMessage) consumer.receive(500000);

Running Receiver.java at this time will make this Receiver.java run for 500 seconds, which can be found in eclipse:

Click on that red square to manually stop running the program.

After running the receiver, we are running the sender. After running the sender, we need to switch to the receiver's console, as shown in the following figure:

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326770391&siteId=291194637