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

Jakarta ee 如何使用JNDI访问嵌入式hornetq,jakarta-ee,jms,jndi,hornetq,Jakarta Ee,Jms,Jndi,Hornetq,我有一个通用生产者程序,它查找jndi资源以连接到jms代理并发送消息 我正在为集成测试设置一个嵌入式hornetq。以下是我用来启动服务器的服务器代码(工作正常) 服务器 Configuration configuration = new ConfigurationImpl(); configuration.setPersistenceEnabled(false); configuration.setSecurityEnabled(false);

我有一个通用生产者程序,它查找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);
Configuration Configuration=new configurationmpl();
configuration.setPersistenceEnabled(false);
配置.setSecurityEnabled(错误);
Map Map=newhashmap();
map.put(“主机”、“本地主机”);
put地图(“港口”,5445);
configuration.getAcceptorConfiguration()
.add(新的传输配置(NettyAcceptorFactory.class.getName(),map));
TransportConfiguration connectorConfig=新的TransportConfiguration(NettyConnectorFactory.class.getName());
getConnectorConfiguration().put(“连接器”,connectorConfig);
//第二步。创建JMS配置
JMSConfiguration jmsConfig=新JMSConfigurationImpl();
//第三步。配置JMS连接工厂
ArrayList connectorNames=新的ArrayList();
连接器名称。添加(“连接器”);
ConnectionFactoryConfiguration cfConfig=新的ConnectionFactoryConfigurationMPL(“ConnectionFactory”,false,connectorNames,“/ConnectionFactory”);
jmsConfig.getConnectionFactoryConfigurations().add(cfConfig);
//第四步。配置JMS队列
JMSQueueConfiguration queueConfig=new JMSQueueConfigurationImpl(“exampleQueue”,null,false,“/queue/exampleQueue”);
jmsConfig.getQueueConfigurations().add(queueConfig);
//第五步。使用HornetQ核心服务器和JMS配置启动JMS服务器
EmbeddedJMS jmsServer=新的EmbeddedJMS();
setConfiguration(配置);
setJmsConfiguration(jmsConfig);
jmsServer.start();
System.out.println(“启动嵌入式JMS服务器”);
制作人

        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);
Hashtable env=new Hashtable();
env.put(Context.PROVIDER\u URL,“jnp://localhost:1099");
put(Context.INITIAL_Context_FACTORY,“org.jnp.interfaces.NamingContextFactory”);
put(Context.URL_PKG_前缀,“org.jboss.naming:org.jnp.interfaces”);
上下文=新的初始上下文(env);
ConnectionFactory ConnectionFactory=(ConnectionFactory)context.lookup(“ConnectionFactory”);
Destination=(Destination)context.lookup(“/queue/exampleQueue”);
Connection=connectionFactory.createConnection();
会话会话=connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
Producer-Producer=session.createProducer(目的地);
connection.start();
TextMessage=session.createTextMessage(“Hello在“+新日期()发送”);
System.out.println(“发送消息:+message.getText());
生产者。发送(消息);

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

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


HornetQ发布了一个简单的嵌入式示例,考虑检查< /p> < p>我使用HoNETQuoTopRestApServer < /P>启动HalNeq

HornetQBootstrapServer hornetQ = new HornetQBootstrapServer(hornetq-beans.xml)
hornetQ.run();
.
.
.
//to stop
hornetQ.shutDown();
我将hornetq-beans.xml、hornetq-configuration.xml和hornetq-jms.xml保存在类路径中

hornetq-beans.xml的示例内容

<bean name="Naming" class="org.jnp.server.NamingBeanImpl"/>
<!-- JNDI server. Disable this if you don't want JNDI -->
<bean name="JNDIServer" class="org.jnp.server.Main">
  <property name="namingInfo">
     <inject bean="Naming"/>
  </property>
  <property name="port">1099</property>
  <property name="bindAddress">localhost</property>
  <property name="rmiPort">1098</property>
  <property name="rmiBindAddress">localhost</property>
</bean>

<!-- MBean server -->
<bean name="MBeanServer" class="javax.management.MBeanServer">
  <constructor factoryClass="java.lang.management.ManagementFactory"
               factoryMethod="getPlatformMBeanServer"/>
</bean> 

.
.
.

1099
本地服务器
1098
本地服务器
.
.
.

这就是我为嵌入式HornetQ启用JNDI的方式。

感谢Francisco,它部分解决了这个问题。我现在只需要找到一种方法来配置嵌入式服务器,以打开一个特定端口,用于与生产者/消费者进行通信,例如端口1099。不知道嵌入式hornetq使用什么默认端口。