我在本地配置后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整合,该如何配置?求助兽总,