Tomcat 9.0.82 - 无法使用 jndi 查找定义的资源

Tomcat 9.0.82 - Unable to locate defined resources using jndi

提问人:Parab 提问时间:11/6/2023 最后编辑:der daParab 更新时间:11/6/2023 访问量:37

问:

我已经正确安装了 Apache Tomcat 和活动 mq - 两者都在默认端口上运行,即默认设置的 8080 和 61616。

我需要为此创建连接工厂和主题,我已经配置了Tomcat的“conf/context.xml”,如下所示。

<Resource name="jms/ConnectionFactory" auth="Container"
      type="org.apache.activemq.ActiveMQConnectionFactory"
      description="JMS Connection Factory"
      factory="org.apache.activemq.jndi.JNDIReferenceFactory"
      brokerURL="tcp://localhost:61616"
      brokerName="LocalActiveMQBroker"/>

 

启动服务器后,Tomcat 日志中未报告任何问题。

现在,我已经配置了 ANT 构建脚本,以便在构建最终的 EAR 包之前执行 Junit。这个 Junit 类“ResourcesLocatorTest”有一个基本工作要做 - 获取初始上下文,获取连接工厂,并获取带有 jndi 名称的主题。找到这些资源后,只需在主题中放置一条消息并断言它。

public class ResourcesLocatorTest {
@Test
public void testJms() throws Exception {
    // Get the initial context
    InitialContext context = new InitialContext();

    // Lookup the connection factory
    ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("jms/ConnectionFactory");

    // Lookup the topic
    Topic topic = (Topic) context.lookup("jms/Topic");

    // Create a connection
    Connection connection = connectionFactory.createConnection();

    // Create a session
    Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

    // Create a message producer
    MessageProducer producer = session.createProducer(topic);

    // Create a message consumer
    MessageConsumer consumer = session.createConsumer(topic);

    // Start the connection
    connection.start();

    // Send a message
    TextMessage message = session.createTextMessage("Hello, World!");
    producer.send(message);

    // Receive a message
    TextMessage receivedMessage = (TextMessage) consumer.receive(1000);

    // Assert that the received message is the same as the sent message
    assertEquals(message.getText(), receivedMessage.getText());

    // Close the resources
    consumer.close();
    producer.close();
    session.close();
    connection.close();
}

}

当此测试在控制台中执行时,NameNotFoundException 被抛出 - 'jms/ConnectionFactory'

我怀疑这里有一些连接问题,因为在我的 JUnit 类中,我没有定义 tomcat URL。如果是,该怎么做?

Java Tomcat

评论


答: 暂无答案