下载
http://activemq.apache.org/activemq-5100-release.html
安装(直接解压)
> tar xzf apache-activemq-5.10.0-bin.tar.gz
> cd apache-activemq-5.10.0/bin
> chmod 755 activemq
启动
> bin/activemq start
查看
apache-activemq-5.10.0> netstat -an|grep61616
管理界面
用户:admin 密码:admin
Java接口
将apache-activemq-5.10.0\lib下的:activemq-client-5.10.0.jar, geronimo-j2ee-management_1.1_spec-1.0.1.jar, geronimo-jms_1.1_spec-1.1.1.jar加入到工程的build path
package com.xxx.comm.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 MsgSender {
private static final int SEND_NUMBER = 5;
public static void main(String[] args) {
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection connection = null;
try {
connection = connectionFactory.createConnection();
connection.start();
//连接会话
Session session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("FirstQueue"); // 创建获取目标消息队列
MessageProducer producer = session.createProducer(destination); // 消息生产者
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 设置是否持久化
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)
throws Exception {
for (int i = 1; i <= SEND_NUMBER; i++) {
TextMessage message = session.createTextMessage("ActiveMq 发送的消息" + i);
System.out.println("发送消息:" + message.getText());
producer.send(message);
}
}
}
package com.xxx.comm.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 MsgReceiver {
public static void main(String[] args) {
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
Connection connection = null;
try {
connection = connectionFactory.createConnection();
connection.start();
//连接会话
Session session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createQueue("FirstQueue"); // 创建获取目标消息队列
MessageConsumer consumer = session.createConsumer(destination); // 消息消费者
while (true) {
//设置接收者接收消息的时间 ms
TextMessage message = (TextMessage) consumer.receive(10000);
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) {
}
}
}
}