一、Window下载ActiveMQ
1. 打开网站
http://activemq.apache.org

官网
2. 安装,启动

安装

启动

启动页面
二、队列模式消息
1. JMS编码接口之间的关系

JMS编码接口之间的关系
2.代码实战
- 添加maven,最新的版本可以去官网找
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
- 编写生产者代码
package market.daoge.active.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* 生产者
* Created by Lidy on 2017/8/26 0026.
*/
public class AppProducer {
private static final String url = "tcp://localhost:61616";
private static final String queueName = "queue-test";
public static void main(String[] args) throws JMSException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2.创建连接
Connection connection = connectionFactory.createConnection();
// 3.启动连接
connection.start();
// 4.创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5.创建一个目标
Destination destination = session.createQueue(queueName);
// 6.创建一个生产者
MessageProducer producer = session.createProducer(destination);
for(int i=0;i<5;i++) {
// 7.创建消息
TextMessage textMessage = session.createTextMessage("text" + i);
// 8.发布消息
producer.send(textMessage);
System.out.println("发送消息" + textMessage.getText());
}
// 9.关闭连接
connection.close();
}
}
- 登录后台查看,用户名和密码默认为admin,admin

activeMQ后台管理

查看队列详情
- 编写消费者代码
package market.daoge.active.queue;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
import javax.xml.soap.Text;
/**
* 消费者
* Created by Lidy on 2017/8/26 0026.
*/
public class AppConsumer {
private static final String url = "tcp://localhost:61616";
private static final String queueName = "queue-test";
public static void main(String[] args) throws JMSException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2.创建连接
Connection connection = connectionFactory.createConnection();
// 3.启动连接
connection.start();
// 4.创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5.创建一个目标
Destination destination = session.createQueue(queueName);
// 6.创建一个消费者
MessageConsumer consumer = session.createConsumer(destination);
// 7.创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message;
try {
System.out.println("接收消息" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
// 9.关闭连接
// connection.close();
}
}
- 结果,运行两个消费者代码,运行一个生产者代码,按照队列的方式

第一个消费者

第二个消费者

生产者
三、主题模式消息
1.代码实战
- 创建生产者
package market.daoge.active.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* 生产者
* Created by Lidy on 2017/8/26 0026.
*/
public class AppProducer {
private static final String url = "tcp://localhost:61616";
private static final String topicName = "topic-test";
public static void main(String[] args) throws JMSException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2.创建连接
Connection connection = connectionFactory.createConnection();
// 3.启动连接
connection.start();
// 4.创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5.创建一个目标
Destination destination = session.createTopic(topicName);
// 6.创建一个生产者
MessageProducer producer = session.createProducer(destination);
for(int i=0;i<5;i++) {
// 7.创建消息
TextMessage textMessage = session.createTextMessage("text" + i);
// 8.发布消息
producer.send(textMessage);
System.out.println("发送消息" + textMessage.getText());
}
// 9.关闭连接
connection.close();
}
}
- 创建消费者
package market.daoge.active.topic;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
/**
* 消费者
* Created by Lidy on 2017/8/26 0026.
*/
public class AppConsumer {
private static final String url = "tcp://localhost:61616";
private static final String topicName = "topic-test";
public static void main(String[] args) throws JMSException {
// 1.创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
// 2.创建连接
Connection connection = connectionFactory.createConnection();
// 3.启动连接
connection.start();
// 4.创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
// 5.创建一个目标
Destination destination = session.createTopic(topicName);
// 6.创建一个消费者
MessageConsumer consumer = session.createConsumer(destination);
// 7.创建一个监听器
consumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage)message;
try {
System.out.println("接收消息" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
// 9.关闭连接
// connection.close();
}
}
- 启动两个消费者,一个生产者,两个消费者同时收到5条信息

生产者

第一个消费者

第二个消费者
网友评论