我正在嘗試爲JMS隊列創建POC。我使用spring mvc控制器作爲jms的客戶端。嘗試將異步偵聽器添加到MessageConsumer對象(代碼段)時出現以下錯誤。我讀過的地方只能將聽衆添加到MDB(消息驅動的bean),這是真的嗎?JMS - 將偵聽器添加到MessageConsumer時出錯
安裝程序:對於JMS使用websphere服務器總線。爲conn工廠,目的地等添加了jndi,以便同步操作一切正常。但是,對於異步,這是行不通的。
用於this用於設置JMS
[1/28/14 14:38:12:570 CST] 0000005d SystemErr R javax.jms.IllegalStateException: CWSIA0084E: The method MessageConsu
mer.setMessageListener is not permitted in this container.
[1/28/14 14:38:12:572 CST] 0000005d SystemErr R at com.ibm.ws.sib.api.jms.impl.JmsMsgConsumerImpl._setMessageListen
er(JmsMsgConsumerImpl.java:660)
[1/28/14 14:38:12:573 CST] 0000005d SystemErr R at com.ibm.ws.sib.api.jms.impl.JmsMsgConsumerImpl.setMessageListene
r(JmsMsgConsumerImpl.java:609)
CODE:從Queue
public void connect(String hostName, String portNumber,
String connectionFactoryString, String consumerJNDIName)
throws Exception {
Hashtable env = new Hashtable();
env.put(Context.PROVIDER_URL, "iiop://" + hostName + ":" + portNumber
+ "");
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.ibm.websphere.naming.WsnInitialContextFactory");
InitialContext initialContext = new InitialContext(env);
ConnectionFactory connectionFactory = (ConnectionFactory) initialContext
.lookup(connectionFactoryString);
connection = connectionFactory.createConnection();
connection.start();
// create destination - JMSQueue
Destination destinationReceiver = (Destination) initialContext
.lookup(consumerJNDIName);
consumerSession = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
consumer = consumerSession.createConsumer(destinationReceiver);
consumer.setMessageListener(new MessageListener() { **// ERROR here**
public void onMessage(Message msg) {
try {
System.out.println("received: " + ((TextMessage) msg).getText());
} catch (JMSException ex) {
ex.printStackTrace();
}
}
});
}
什麼是您的WebSphere Application Server版本? –
WS 6.1 – Zeus
這是什麼意思 - 「對於異步,這不工作」? JMS本身就是異步協議。您實施的示例並非真正正確。你需要使用來自JMS隊列的消息嗎? –