linux平台ActiveMQ搭建
- 使用浏览器或工具
(例如wget,scp,ftp等)将activemq压缩的tarball文件下载到Unix机器上,例如:(请参见下载 ->“最新的稳定版本”)
wget http://activemq.apache.org/path/tofile/apache-activemq-x.x.x-bin.tar.gz
- 将文件从压缩的tarball解压缩到您选择的目录中。例如:
cd [activemq_install_dir]
tar -zxvf activemq-x.x.x-bin.tar.gz
- 尝试启动
cd [activemq_install_dir]
bin/activemq start
初使用
- 队列模式
- 生产者
public class MessageProducer {
//定义ActivMQ的连接地址
private static final String BROKER_URL = "tcp://127.0.0.1:61616";
//定义发送消息的队列名称
private static final String QUEUE_NAME = "hoas-test-queue";
public static void main(String[] args) {
//创建连接
Connection connection = null;
try {
//创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
activeMQConnectionFactory.setUserName("system");
activeMQConnectionFactory.setPassword("manager");
//创建连接
connection = activeMQConnectionFactory.createConnection();
//打开连接
connection.start();
//创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建队列目标
Destination destination = session.createQueue(QUEUE_NAME);
//创建一个生产者
javax.jms.MessageProducer producer = session.createProducer(destination);
//创建模拟100个消息
for (int i = 1 ; i <= 100 ; i++){
TextMessage message = session.createTextMessage("我发送message:" + i);
//发送消息
producer.send(message);
//在本地打印消息
System.out.println("我现在发的消息是:" + message.getText());
}
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭连接
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
connection = null;
}
}
}
}
}
- 消费者
public class MessageConsumer {
//定义ActivMQ的连接地址
private static final String BROKER_URL = "tcp://127.0.0.1:61616";
//定义发送消息的队列名称
private static final String QUEUE_NAME = "hoas-test-queue";
public static void main(String[] args) throws JMSException {
//创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
activeMQConnectionFactory.setUserName("system");
activeMQConnectionFactory.setPassword("manager");
//创建连接
Connection connection = activeMQConnectionFactory.createConnection();
//打开连接
connection.start();
//创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建队列目标
Destination destination = session.createQueue(QUEUE_NAME);
//创建消费者
javax.jms.MessageConsumer consumer = session.createConsumer(destination);
//创建消费的监听
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("获取消息:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
}
}
- 主题模式
- 发布者
public class MessagePulisher {
//定义ActivMQ的连接地址
private static final String BROKER_URL = "tcp://127.0.0.1:61616";
//定义发送消息的主题名称
private static final String TOPIC_NAME = "hoas-test-topic";
public static void main(String[] args) throws JMSException {
//创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
activeMQConnectionFactory.setUserName("system");
activeMQConnectionFactory.setPassword("manager");
//创建连接
Connection connection = activeMQConnectionFactory.createConnection();
//打开连接
connection.start();
//创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建队列目标
Destination destination = session.createTopic(TOPIC_NAME);
//创建一个生产者
javax.jms.MessageProducer producer = session.createProducer(destination);
//创建模拟100个消息
for (int i = 1; i <= 100; i++) {
TextMessage message = session.createTextMessage("当前message是(主题模型):" + i);
//发送消息
producer.send(message);
//在本地打印消息
System.out.println("我现在发的消息是:" + message.getText());
}
//关闭连接
connection.close();
}
}
- 订阅者
public class MessageSubscriber {
//定义ActivMQ的连接地址
private static final String BROKER_URL = "tcp://127.0.0.1:61616";
//定义发送消息的主题名称
private static final String TOPIC_NAME = "hoas-test-topic";
public static void main(String[] args) throws JMSException {
//创建连接工厂
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(BROKER_URL);
activeMQConnectionFactory.setUserName("system");
activeMQConnectionFactory.setPassword("manager");
//创建连接
Connection connection = activeMQConnectionFactory.createConnection();
//打开连接
connection.start();
//创建会话
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建主题目标
Destination destination = session.createTopic(TOPIC_NAME);
//创建消费者
javax.jms.MessageConsumer consumer = session.createConsumer(destination);
//创建消费的监听
consumer.setMessageListener(new MessageListener() {
@Override
public void onMessage(Message message) {
TextMessage textMessage = (TextMessage) message;
try {
System.out.println("获取消息:" + textMessage.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
});
}
}
归纳
- 使用ActiveMQ主要有以下几步
- 创建连接工厂(ActiveMQConnectionFactory)
- 创建连接(Connection)
- 开起连接
- 创建会话(Session)
- 指定一个目标,如果指定的目标不存在,将自动创建,指定的目标可以使队列或者主题(Destination)
- 针对指定的目标创建生产者/发布者或消费者/订阅者
- 进行消息的发送或接收工作
- 操作完毕后关闭连接释放资源(一般情况下监听者连接会一直挂起等待消息的到来)
一些补充
- ActiveMQConnectionFactory 有时候为了安全问题,在MQ访问的时候增加了认证信息,在 activemq.xml 文件中 找到 标签,增加如下话即可
<!--
The <broker> element is used to configure the ActiveMQ broker.
-->
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
<plugins>
<simpleAuthenticationPlugin>
<users>
<authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/>
</users>
</simpleAuthenticationPlugin>
</plugins>
...
</broker>
-
connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 参数含义
创建了一个session,需要设置两个参数,第一个参数是是否支持事务,第二个参数是设置签收模式。签收模式 主要有
AUTO_ACKNOWLEDGE: 当消息发送给接收者后自动签收
CLIENT_ACKNOWLEDGE:接收者接收到消息后手动调用acknowledge()方法进行签收
DUPS_OK_ACKNOWLEDGE:此选项指示Session不必确保对传送消息的签收。他可能引起消息的重复,但是降低了Session的开销,所以只有对客户端能容忍重复的消息,才可使用。 -
信息的接收者除了采用Listener方式外,还有另外的方式
Message message = consumer.receive();
这种方式将接收一次消息,如果没有消息,将会阻塞等待。
参考:
- https://blog.csdn.net/qq_39705793/article/details/88312954
- https://www.cnblogs.com/rainwang/p/5146065.html