第一步点开新建然后other 里面的maven里面的maven project。groud id 与packed需要前面写com.xxxxx。
新建完成。
2.在pom.xml添加依赖
添加完后。添加jar
在java里面添加
先new一下。之后添加外部jar
新建java文件
新建后添加此代码
- 消息生产者TopicPublisher.java
package com.activemq.ActiveMQ;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class TopicPublisher {
public static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
public static final String BROKER_URL=ActiveMQConnection.DEFAULT_BROKER_URL;
public static void main(String[] args) {
//创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);
try {
//创建连接
Connection connection = connectionFactory.createConnection();
//开启连接
connection.start();
//创建会话,不需要事务
Session session = connection. createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建Topic, 用作消费者订阅消息
Topic myTestTopic = session.createTopic("activemq-topic-test1");
//消息生产者.
MessageProducer producer = session.createProducer(myTestTopic);
for (int i= 1;i<= 3;i++){
TextMessage message = session.createTextMessage("发送消息"+ i);
producer. send(myTestTopic, message);
}
//关闭资源
session.close();
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
2.继续添加
消息消费者TopicSubscriber.java
package com.activemq.ActiveMQ;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import javax.jms.*;
public class TopicSubscriber {
public static final String USERNAME = ActiveMQConnection.DEFAULT_USER;
public static final String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
public static final String BROKER_URL=ActiveMQConnection.DEFAULT_BROKER_URL;
public static void main(String[] args) {
//创建连接工厂
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);
try {
//创建连接
Connection connection = connectionFactory.createConnection();
//开启连接
connection.start();
//创建会话,不需要事务
Session session = connection. createSession(false, Session.AUTO_ACKNOWLEDGE);
//创建Topic, 用作消费者订阅消息
Topic myTestTopic = session.createTopic("activemq-topic-test1");
//消息生产者.
MessageConsumer messageConsumer=session.createConsumer(myTestTopic);
messageConsumer.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
System.out.println("消费者1接收到消息: " + ((TextMessage) message).getText());
} catch (JMSException e) {
e.printStackTrace();
}}});
MessageConsumer messageConsumer2 = session.createConsumer(myTestTopic);
messageConsumer2.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try{
System.out.println("消费者2接收到消息: " + ((TextMessage) message).getText());
} catch (JMSException e) {
e.printStackTrace();
}}});
MessageConsumer messageConsumer3 = session.createConsumer(myTestTopic);
messageConsumer3.setMessageListener(new MessageListener() {
public void onMessage(Message message) {
try {
System.out.println("消费者3接收到消息: " + ((TextMessage) message).getText());
} catch (JMSException e) {
e.printStackTrace();
}}});
//让主线程休眠100秒,使消息消费者对象能继续存活一段时间从而能监听到消息
Thread.sleep(100*1000);
//关闭资源
session.close();
connection.close();
}catch (Exception e) {
e.printStackTrace();
}
}
}
打开bin目录下的
启动activeMQ 以管理员身份运行。
- 启动activemq服务器
http://127.0.0.1:8161/admin/
用户名:admin
密码:admin
- 运行TopicSubscriber
INFO | Successfully connected to tcp://localhost:61616