我正在尝试使用ActiveMQ嵌入式代理编写我的第一个最简单的JMS应用程序。该应用程序是一个sping桌面应用程序,这是它的主要方法:
public static void main( String[] args ) throws JMSException
{
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/appContext.xml");
ConnectionFactory cf = (ConnectionFactory) context.getBean("jmsFactory");
Connection connetcion = null;
try{
connetcion = cf.createConnection();
final Session session = connetcion.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination testQueue = (Destination) context.getBean("testQueue");
String text = "Test message";
MessageProducer mp = session.createProducer(testQueue);
Message msg = session.createTextMessage(text);
mp.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
mp.send(msg);
MessageConsumer mc = cf.createConnection().createSession(false, Session.AUTO_ACKNOWLEDGE).createConsumer(testQueue);
TextMessage recievedMsg = null;
recievedMsg = (TextMessage) mc.receive(); //1 <---------- Here is the problem
System.out.println(recievedMsg.getText());
} catch(JMSException jmse){
throw new AssertionError("JMS Exception");
}
finally{
connetcion.close();
}
}
spring配置包含以下bean:
<amq:broker useJmx="false" persistent="false">
<amq:transportConnectors>
<amq:transportConnector uri="tcp://localhost:61616" />
</amq:transportConnectors>
</amq:broker>
<amq:queue id="testQueue" physicalName="com.badmitrii.test" />
<amq:connectionFactory id="jmsFactory" brokerURL="tcp://localhost:61616" />
我尝试向队列发送消息,然后在同一个线程中使用该消息。但是当我在receive()
调用//1
方法时,主线程ios被阻塞,就像队列中没有任何mesasge一样。
我做错了什么?
答案 0 :(得分:1)
看起来你没有开始()你的连接。
connetcion = cf.createConnection();
connetcion.start();
应该做的伎俩。