TomEE Con​​fig问题,jndi.properties,NameNotFoundException

时间:2016-07-22 10:10:32

标签: java server config jndi tomee

正如标题已经显示的那样,我坚持使用我的服务器配置。目标是建立一个类,作为发布者和许多特定主题的订阅者。我在TomEE上运行,因为整个环境都在tomcat上,而TomEE提供了JMS API。

现在代码:

javax.naming.NameNotFoundException: Name [TopicConnectionFactory] is not bound in this Context. Unable to find [TopicConnectionFactory].

这是我移动到浏览器中调用方法的.jsp文件时获得的重复。

出版商:

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();
            }
        }
    }   
}
在.jsp

中调用

doIt()

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();
            }
        }
    }

}

最后但并非最不重要的是在src文件夹中的jndi.properties文件...所以在构建路径中

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

这是复制的,因为我完全不知道如何完成这种查找机制。如果您发现任何错误,请相应更正!

1 个答案:

答案 0 :(得分:0)

如果您在tomee.xml中定义了TopicConnectionFactory,则该名称可能是: openejb:Resource/TopicConnectionFactory