NutzCN Logo
问答 关于activeMQ配置的问题
发布于 2918天前 作者 qq_6c186ef5 2068 次浏览 复制 上一个帖子 下一个帖子
标签: activeMQ

我在本地配置后activemq服务器之后,在java项目中直接右键run的话是开源正常监听消息,但是在web项目中就无效了,求助,如何处理

public class ConnectUtil {
	
	//默认的用户名
	private static String userName = ActiveMQConnection.DEFAULT_USER;
	//默认的用户密码
	private static String pwd  = ActiveMQConnection.DEFAULT_PASSWORD;
	//默认的链接地址
	private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
	//默认主题
	private static String subject;
	
	public static void setSubject(String... subject) {
		if(null==subject)
			ConnectUtil.subject = "Tool.DEFAULT";
		else
			ConnectUtil.subject = subject[0];
	}
	
	public static String getSubject() {
		return ConnectUtil.subject;
	}
	
	/**
	 * 该方法返回了用户名,密码,链接地址,主题
	 * @return
	 */
	public static String[] getConnInfo(){
		ConnectUtil.setSubject(null);
		String[] info = new String[]{userName,pwd,url,ConnectUtil.getSubject()};
		return info;
	}
	/**
	 * 获取链接工厂
	 * @return
	 */
	public static ConnectionFactory getConnFactory(){
		String[] info = new String[]{userName,pwd,url,subject};
		//创建消息连接工厂对象
		ConnectionFactory cf = new ActiveMQConnectionFactory(info[0], info[1], info[2]);
		return cf;
	}
	
	
}
public class Publisher {
	
	public static void main(String[] args) {
		String[] info = ConnectUtil.getConnInfo();
		//创建消息连接工厂对象
		ConnectionFactory cf = new ActiveMQConnectionFactory(info[0], info[1], info[2]);
		//通过工厂对象创建连接
		Connection conn;
		try {
			conn = cf.createConnection();
			conn.start();
			Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			//创建topic
			Topic topic = session.createTopic(info[3]);
			//通过主题创建消息的生成者
			MessageProducer mp = session.createProducer(topic);
			//设置消息的交付/递送方式
			mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
//			for (int i = 0; i <= 20; i++) {
				MapMessage mapMsg = session.createMapMessage();
				Date date = new Date();
				mapMsg.setLong("count", date.getTime());
				mapMsg.setString("login", "恭喜【用户"+(1)+"】订阅消息成功");
				//线程休眠1秒
				Thread.sleep(1000);
				mp.send(mapMsg);
				System.out.println("---我要发送消息了123:--"+date);
				
//			}
			session.commit();
			session.close();
			conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
public class SubscriberFirst {
	
	public static void main(String[] args) {
		
		ConnectionFactory cf = ConnectUtil.getConnFactory();
		
		Connection conn;
		try {
			ConnectUtil.setSubject(null);
			conn = cf.createConnection();
			conn.start();
			final Session session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic(ConnectUtil.getSubject());
			MessageConsumer consumer = session.createConsumer(topic);
			consumer.setMessageListener(new MessageListener() {
				
				@Override
				public void onMessage(Message msg) {
					
					MapMessage mapMsg = (MapMessage) msg;
					try {
						//System.out.println("订阅者1收到一条消息"+new Date(mapMsg.getLong("count")));
						System.out.println("订阅者:"+mapMsg.getString("login"));
						session.commit();
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});
			//session.close();
			//conn.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
	
}

这个代码就是简单的测试代码,如果要与nutzMVC整合,该如何配置?求助兽总,

7 回复

是不是能注入ActiveMQConnectionFactory实例,剩下的就能搞定了??

在设置消息监听的匿名内部类中的方法根本就不执行

consumer.setMessageListener(new MessageListener() {
				
				@Override
				public void onMessage(Message msg) {
					
					MapMessage mapMsg = (MapMessage) msg;
					try {
						//System.out.println("订阅者1收到一条消息"+new Date(mapMsg.getLong("count")));
						System.out.println("订阅者:"+mapMsg.getString("login"));
						session.commit();
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});

ActiveMQConnectionFactory的实例是有的org.apache.activemq.ActiveMQConnectionFactory@150a19a

web环境下,也是调用main方法吗?

就是通过其他方法去直接调用main方法

不是,是重新分装的,分装成两个类中的静态方法,代码与此相同,只不过闯入了执行数据库成功后的受影响行数,

public class UsersregSubscriber {
	
	
	private static final Logger log = Logger.getLogger("Subscriber");
	private static Map<String,Object> map2s = null;
	public static void subscriber(final Map<String,Object> map2){
		ConnectionFactory cf = ConnectUtil.getConnFactory();
		Connection conn = null;
		Session session = null;
		try {
			ConnectUtil.setSubject(null);
			conn = cf.createConnection();
			conn.start();
			session = conn.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE);
			Topic topic = session.createTopic(ConnectUtil.getSubject());
			MessageConsumer consumer = session.createConsumer(topic);
			//UsersregSubscriber sub = new UsersregSubscriber();
			consumer.setMessageListener(new MessageListener() {
				public void onMessage(Message message) {
					MapMessage mapMsg = (MapMessage) message;
					if(mapMsg!=null){
						try {
							long state = mapMsg.getLong("state");
							if(state>0){
								//执行相应的操作
								map2.put("desc", mapMsg.getString("desc"));
								UsersregSubscriber.setMap2(map2);
								log.info("收到的消息:"+mapMsg.getString("desc"));
							}
						} catch (JMSException e) {
							e.printStackTrace();
						}
					}
				}
			});
			log.info("消息订阅成功");
		} catch (Exception e) {
			e.printStackTrace();
			log.info("消息订阅失败!");
		}finally{
			//FlushResource fr = new FlushResource(conn, session);
			//fr.closeNoMapMsg();
		}
	}
	
	public static Map<String, Object> getMap2() {
		return map2s;
	}
	public static void setMap2(Map<String, Object> map2) {
		map2s = map2;
	}
	
}

我觉得嘛, 你还是调用main方法试试吧

添加回复
请先登陆
回到顶部