as the title already shows, I am stuck with my server configuration. Goal is to have a class which will be acting as publisher and lots of subscribers to a specific topic. I am running on TomEE because the whole environment is on tomcat, and TomEE delivers the JMS API.
Now to the code:
javax.naming.NameNotFoundException: Name [TopicConnectionFactory] is not bound in this Context. Unable to find [TopicConnectionFactory].
that is the excpetion I get when I move to the .jsp file inside my browser which calls the method.
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() is called inside the .jsp
SUBSCRIBER
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();
}
}
}
}
AND last but not least the jndi.properties file which is inside src folder...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
This is copied because I have absolutely no idea how to accomplish this lookup mechanism. If you see anything wrong, please, correct it accordingly!
如果您在tomee.xml
定义了TopicConnectionFactory,则可能使用以下名称: openejb:Resource/TopicConnectionFactory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.