ActiveMQ 如何获取服务器已有topic
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
1个回答
展开全部
package test;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ReceiverTest implements MessageListener{
public static void main(String[] args) {
// TODO Auto-generated method stub
new ReceiverTest().receiver();
}
public void receiver(){
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection connection = null;
Session session = null;
try{
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("myTopic");//和queue就这一点区别
//Destination destination = session.createQueue("myQueue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(this);
}catch(Exception e){
e.printStackTrace();
try {
connection.close();
} catch (JMSException e1) {
e1.printStackTrace();
}
}
}
@Override
public void onMessage(Message message) {
ObjectMessage msg = (ObjectMessage)message;
try {
System.out.println("ReceiverMessage:"+msg.getObject());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnectionFactory;
public class ReceiverTest implements MessageListener{
public static void main(String[] args) {
// TODO Auto-generated method stub
new ReceiverTest().receiver();
}
public void receiver(){
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
Connection connection = null;
Session session = null;
try{
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic("myTopic");//和queue就这一点区别
//Destination destination = session.createQueue("myQueue");
MessageConsumer consumer = session.createConsumer(destination);
consumer.setMessageListener(this);
}catch(Exception e){
e.printStackTrace();
try {
connection.close();
} catch (JMSException e1) {
e1.printStackTrace();
}
}
}
@Override
public void onMessage(Message message) {
ObjectMessage msg = (ObjectMessage)message;
try {
System.out.println("ReceiverMessage:"+msg.getObject());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是一家可靠的代理服务提供商,提供原生IP(住宅原生IP)和高匿名代理服务。以下是关于StormProxies的原生IP服务的一些信息:1. 住宅原生IP:StormProxies提供的住宅原生IP是指从真实的家庭或企...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询