0
作爲標題已經顯示,我堅持我的服務器配置。目標是擁有一個充當發佈者的類,併爲特定主題提供大量訂閱者。我在TomEE上運行,因爲整個環境都在tomcat上,TomEE提供了JMS API。TomEE配置問題,jndi.properties,NameNotFoundException
我們的代碼:
javax.naming.NameNotFoundException: Name [TopicConnectionFactory] is not bound in this Context. Unable to find [TopicConnectionFactory].
那是錯誤時拋出我得到當我移動到.jsp文件瀏覽器中調用該方法內。
PUBLISHER:
public void doIt(){
String _topicName = null;
Context _jndiContext = null;
TopicConnectionFactory _topicConnectionFactory=null;
TopicConnection _topicConnection = null;
TopicSession _topicSession= null;
Topic _topic = null;
TopicPublisher _topicPublisher = null;
TextMessage _textMessage = null;
_topicName = "Events";
try {
_jndiContext = new InitialContext();
_topicConnectionFactory = (TopicConnectionFactory) _jndiContext.lookup("TopicConnectionFactory");
_topic = (Topic) _jndiContext.lookup(_topicName);
_topicConnection = _topicConnectionFactory.createTopicConnection();
_topicSession = _topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
_topicPublisher = _topicSession.createPublisher(_topic);
for(int i = 0; i < 1500; i++){
_textMessage = _topicSession.createTextMessage("This i message: "+ i);
_topicPublisher.publish(_textMessage);
}
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(_topicConnection != null){
try{
_topicConnection.close();
}catch(JMSException e){
e.printStackTrace();
}
}
}
}
doIt方法()被調用這個.jsp
訂戶
public static void main(String [] args){
String _topicName = null;
Context _jndiContext = null;
TopicConnectionFactory _topicConnectionFactory = null;
TopicConnection _topicConnection = null;
TopicSession _topicSession = null;
Topic _topic = null;
TopicSubscriber _topicSubscriber = null;
TextListener _topicListener = null;
TextMessage _textMessage = null;
_topicName = "Events";
try {
_jndiContext = new InitialContext();
_topicConnectionFactory =(TopicConnectionFactory) _jndiContext.lookup("TopicConnectionFactory");
_topic = (Topic) _jndiContext.lookup(_topicName);
_topicConnection = _topicConnectionFactory.createTopicConnection();
_topicSession = _topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
_topicSubscriber = _topicSession.createSubscriber(_topic);
_topicListener = new TextListener();
_topicSubscriber.setMessageListener(_topicListener);
_topicConnection.start();
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(_topicConnection != null){
try{
_topicConnection.close();
}catch(JMSException e){
e.printStackTrace();
}
}
}
}
和最後但並非最不重要的jndi.properties文件,該文件是src文件夾內內。 .. so inside build path
java.naming.factory.initial = org.apache.activemq.jndi.ActiveMQInitialContextFactory
java.naming.provider.url = tcp://localhost:8080
connectionFactoryNames = ConnectionFactory, queueConnectionFactory, TopicConnectionFactory
queue.MyQueue = example.MyQueue
topic.MyTopic = Events
這是因爲我完全不知道如何完成這種查找機制。如果您發現任何錯誤,請相應更正!
忽略其內部的for循環中產生的內容......它只是用於測試,如果消息將被轉移 –