ActiveMQ 无法创建连接
手写的一个ActiveMQ的实例。在linux虚机上安装了ActiveMQ服务。服务地址为:http://192.168.116.128:8161/admin;
下面贴代码:
消息提供者:
package com.taotao.manager.MQ;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSProducer {
//默认链接的用户名
private static final String USERNAME = "admin";
//默认的链接密码
private static final String PASSWORD = "admin";
//默认的链接地址
private static final String BROKEURL = "http://192.168.116.128:8161/admin";
//发送的消息数量
private static final int count =10;
public static void main(String[] args){
//连接工厂
ConnectionFactory connectionFactory = null;
//连接
Connection connection =null;
//会话
Session session =null;
//消息的目的地
Destination destination;
//消息的生产者
MessageProducer messageProducer;
//实例化工厂
connectionFactory = new ActiveMQConnectionFactory(JMSProducer.USERNAME,JMSProducer.PASSWORD,JMSProducer.BROKEURL);
try {
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建session
session = connection.createSession(true,Session.AUTO_ACKNOWLEDGE);
//创建一个名为HelloWorld的消息队列
destination = session.createQueue("消息队列Hello World");
//创建消息生成者
messageProducer = session.createProducer(destination);
//发送消息
try {
sendMessage(session,messageProducer);
} catch (Exception e) {
e.printStackTrace();
}
} catch (JMSException e) {
e.printStackTrace();
}
}
private static void sendMessage(Session session,MessageProducer messageProducer) throws Exception{
for(int i=0;i<JMSProducer.count;i++){
//创建一条文本消息
TextMessage textMessage = session.createTextMessage("ActiveMQ 发送消息" +i);
System.out.println("发送消息:ActiveMQ 发送消息" +i );
//通过消息生成者发送消息
messageProducer.send(textMessage);
}
}
}
消息消费者:
package com.taotao.manager.MQ;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSconsumer {
//默认链接的用户名
private static final String USERNAME = "admin";
//默认的链接密码
private static final String PASSWORD = "admin";
//默认的链接地址
private static final String BROKEURL = "http://192.168.116.128:8161/admin";
public static void main(String[] args){
ConnectionFactory connectionFactory =null;
Connection connection =null;
Destination destination =null;
Session session =null;
MessageConsumer messageConsumer =null;
//实例化工厂
connectionFactory = new ActiveMQConnectionFactory(JMSconsumer.USERNAME,JMSconsumer.PASSWORD,JMSconsumer.BROKEURL);
try {
//创建连接
connection = connectionFactory.createConnection();
//启动连接
connection.start();
//创建会话
session = connection.createSession(true, session.AUTO_ACKNOWLEDGE);
//创建目的地
destination = session.createQueue("hello world");
messageConsumer = session.createConsumer(destination);
while(true){
TextMessage textMessage = (TextMessage)messageConsumer.receive(100000);
if(textMessage!=null){
System.out.println("收到的消息" + textMessage.getText());
}else{
break;
}
}
} catch (JMSException e) {
e.printStackTrace();
}
}
}
javax.jms.JMSException: Could not connect to broker URL: http://192.168.116.128:8161/admin. Reason: java.io.IOException: Failed to perform GET on: http://192.168.116.128:8161/admin as response was: Unauthorized
at org.apache.activemq.util.JMSExceptionSupport.create(JMSExceptionSupport.java:36)
at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:358)
at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:303)
at org.apache.activemq.ActiveMQConnectionFactory.createConnection(ActiveMQConnectionFactory.java:243)
at com.taotao.manager.MQ.JMSProducer.main(JMSProducer.java:41)
Caused by: java.io.IOException: Failed to perform GET on: http://192.168.116.128:8161/admin as response was: Unauthorized
at org.apache.activemq.transport.http.HttpClientTransport.doStart(HttpClientTransport.java:279)
at org.apache.activemq.util.ServiceSupport.start(ServiceSupport.java:55)
at org.apache.activemq.transport.AbstractInactivityMonitor.start(AbstractInactivityMonitor.java:138)
at org.apache.activemq.transport.TransportFilter.start(TransportFilter.java:58)
at org.apache.activemq.transport.TransportFilter.start(TransportFilter.java:58)
at org.apache.activemq.ActiveMQConnectionFactory.createActiveMQConnection(ActiveMQConnectionFactory.java:338)
... 3 more
显示的是认证不通过,但是我直接在浏览器中输入地址是可以访问active控制台的。求教是不是在创建连接的时候需要做什么认证
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
已经解决了,谢谢
这个地址web控制台地址,你的消息发送地址不是这个