ActiveMQ发送消息出现连接失效问题
最近玩起了Docker和ActiveMQ之后,想起可以在Docker中搭建一个ActiveMQ容器来运行ActiveMQ环境,当我费劲千辛终于搭建好了容器之后,我照着老师的代码做了一遍,发现出现了连接失败问题,起初我以为是我的容器端口映射出现了问题,我在本地连接容器是完全没有问题的,然后我翻阅ActiveMQ的文档发现使用的传送协议是TCP协议,我有回头将容器的TCP协议端口打开了,发现还是不行,请大家帮我看看是哪里出现了问题。
下面是我程序主要代码
private static final String MQ_USER = ActiveMQConnection.DEFAULT_USER;
private static final String MQ_PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;
private static final String MQ_ADDRESS = "tcp://192.168.1.106:32769";
private static final int SEND_NUMBER = 50;
public static void main(String[] args){
Connection connection = null;
Session session = null;
try {
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
MQ_USER, MQ_PASSWORD, MQ_ADDRESS);
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
//消息接受者,消息发送目的地
Destination destination = session.createQueue("testSendQueue1");
//消息发送者
MessageProducer producer = session.createProducer(destination);
//不进行持久化
// producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
sendMessage(session, producer);
session.commit();
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(session != null){
session.close();
}
if(connection != null){
connection.close();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
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("发送消息:" + "ActiveMq 发送的消息 : " + i);
producer.send(message);
}
}