jakarta-ee - 如何使用JNDI访问嵌入式hornetq

标签 jakarta-ee jms jndi hornetq

我有一个通用的生产者程序,它查找 jndi 资源以连接到 jms 代理并发送消息。

我正在为集成测试设置一个嵌入式 hornetq。这是我用来启动服务器的服务器代码(工作正常)

服务器

        Configuration configuration = new ConfigurationImpl();
        configuration.setPersistenceEnabled(false);
        configuration.setSecurityEnabled(false);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("host", "localhost");
        map.put("port", 5445);
        configuration.getAcceptorConfigurations()
                .add(new TransportConfiguration(NettyAcceptorFactory.class.getName(), map));

        TransportConfiguration connectorConfig = new TransportConfiguration(NettyConnectorFactory.class.getName());

        configuration.getConnectorConfigurations().put("connector", connectorConfig);


        // Step 2. Create the JMS configuration
        JMSConfiguration jmsConfig = new JMSConfigurationImpl();

        // Step 3. Configure the JMS ConnectionFactory
        ArrayList<String> connectorNames = new ArrayList<String>();
        connectorNames.add("connector");
        ConnectionFactoryConfiguration cfConfig = new ConnectionFactoryConfigurationImpl("ConnectionFactory", false,  connectorNames, "/ConnectionFactory");
        jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);

        // Step 4. Configure the JMS Queue
        JMSQueueConfiguration queueConfig = new JMSQueueConfigurationImpl("exampleQueue", null, false, "/queue/exampleQueue");
        jmsConfig.getQueueConfigurations().add(queueConfig);

        // Step 5. Start the JMS Server using the HornetQ core server and the JMS configuration
        EmbeddedJMS jmsServer = new EmbeddedJMS();
        jmsServer.setConfiguration(configuration);
        jmsServer.setJmsConfiguration(jmsConfig);

        jmsServer.start();
        System.out.println("Started Embedded JMS Server");

制作人

Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.PROVIDER_URL, "jnp://localhost:1099");
env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
env.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

Context context = new InitialContext(env);

ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("ConnectionFactory");
Destination destination = (Destination) context.lookup("/queue/exampleQueue");

Connection connection = connectionFactory.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Producer producer = session.createProducer(destination);
connection.start();

TextMessage message = session.createTextMessage("Hello sent at " + new Date());
System.out.println("Sending message: " + message.getText());
producer.send(message);

如何为服务器注册 JNDI 资源,以便生产者可以使用这些属性访问嵌入式服务器。

最佳答案

乍一看,您似乎使用 "/cf" 作为绑定(bind)在您的 ConnectionFactoryConfigurationImpl 上创建了一个绑定(bind),但在您的生产者代码中您查找了 “ConnectionFactory”

HornetQ 提供了一个简单 嵌入式示例,考虑查看一下 https://github.com/hornetq/hornetq/tree/2.3.x/examples/jms/embedded-simple

关于jakarta-ee - 如何使用JNDI访问嵌入式hornetq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17368109/

相关文章:

maven - 如何创建 Java EE 7 Maven IntelliJ 项目

jakarta-ee - 访问容器管理的 bean 中的事务状态

java - 如何做包含JDBC和JMS事务的XA事务?

java - 无法通过 JAVA 中的 LDAP 连接到 Active Directory 服务

websphere - 如何在 Websphere 7 JNDI 中配置字符串值?

java - 如何处理 RESTful Web 服务中的聚合和组合

java - 创建WAR打包的maven项目时, 'Deployed Resources'文件夹是什么?

multithreading - 如何通过 tomcat 8 中的 java 代码动态设置系统属性(而不是通过 tomcat 配置文件)

java - Weblogic JMS 客户端 - 在单个事务中从多个队列读取

java - Oracle Advanced Queue - 出队后删除消息