Question

I have a generic producer program that looks up jndi resources to connect to the jms broker and sends the messages.

I am setting an embedded hornetq for integration tests. Here is the server code that I use to start the server (works fine)

Server

        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");

Producer

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

How do I register the JNDI resources for the server so that the producer can access the Embedded server with those properties.

Was it helpful?

Solution 2

I am starting hornetQ by using the HornetQBootstrapServer

HornetQBootstrapServer hornetQ = new HornetQBootstrapServer(hornetq-beans.xml)
hornetQ.run();
.
.
.
//to stop
hornetQ.shutDown();

I keep hornetq-beans.xml, hornetq-configuration.xml and hornetq-jms.xml in the classpath.

Sample contents of 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> 

.
.
.

This is the way I enabled JNDI for the embedded HornetQ.

OTHER TIPS

At a first look, it seems you create a binding at your ConnectionFactoryConfigurationImpl using "/cf" as binding, but in your producer code you look-up for "ConnectionFactory".

HornetQ ships a simple embedded example, consider checking it out https://github.com/hornetq/hornetq/tree/2.3.x/examples/jms/embedded-simple

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top