ActiveMQ 无法创建连接

发布于 2021-12-04 10:35:35 字数 5090 浏览 918 评论 2

手写的一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

飘然心甜 2021-12-04 21:52:30

已经解决了,谢谢

眼眸 2021-12-04 16:55:31

这个地址web控制台地址,你的消息发送地址不是这个

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文