Write a Java Program to connect to HornetQ Messaging Service in Jboss EAP 6.3?

3.2k views Asked by At

I am using Jboss EAP 6.3 and need to use Messaging facility. I have worked in Jboss 4x, where we can make connection easily using following code:

public static final String PROVIDER_URL = "jnp://localhost:5445";
   public static final String JNP_INTERFACES = "org.jboss.naming:org.jnp.interfaces";
   public static final String INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";

   private static Context initialContext;

   public static Context getInitialContextForClient() throws NamingException{
          if(initialContext == null){
                 Properties prop = new Properties();
                 prop.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
                 prop.put(Context.URL_PKG_PREFIXES,JNP_INTERFACES);
                 prop.put(Context.PROVIDER_URL, PROVIDER_URL);
                 initialContext = new InitialContext(prop);
          }
          return initialContext;
   }

Will above way work in EAP 6.3 to connect to HornetQ? If yes, what other configurations are required? Also, i found 1099 is also not configured by default in standalone.xml.

Following are by default settings done for HornetQ in Standalone-full.xml file:

<subsystem xmlns="urn:jboss:domain:messaging:1.4">
        <hornetq-server>
            <persistence-enabled>true</persistence-enabled>
            <journal-type>NIO</journal-type>
            <journal-min-files>2</journal-min-files>

            <connectors>
                <netty-connector name="netty" socket-binding="messaging"/>
                <netty-connector name="netty-throughput" socket-binding="messaging-throughput">
                    <param key="batch-delay" value="50"/>
                </netty-connector>
                <in-vm-connector name="in-vm" server-id="0"/>
            </connectors>

            <acceptors>
                <netty-acceptor name="netty" socket-binding="messaging"/>
                <netty-acceptor name="netty-throughput" socket-binding="messaging-throughput">
                    <param key="batch-delay" value="50"/>
                    <param key="direct-deliver" value="false"/>
                </netty-acceptor>
                <in-vm-acceptor name="in-vm" server-id="0"/>
            </acceptors>

            <security-settings>
                <security-setting match="#">
                    <permission type="send" roles="guest"/>
                    <permission type="consume" roles="guest"/>
                    <permission type="createNonDurableQueue" roles="guest"/>
                    <permission type="deleteNonDurableQueue" roles="guest"/>
                </security-setting>
            </security-settings>

            <address-settings>
                <address-setting match="#">
                    <dead-letter-address>jms.queue.DLQ</dead-letter-address>
                    <expiry-address>jms.queue.ExpiryQueue</expiry-address>
                    <redelivery-delay>0</redelivery-delay>
                    <max-size-bytes>10485760</max-size-bytes>
                    <page-size-bytes>2097152</page-size-bytes>
                    <address-full-policy>PAGE</address-full-policy>
                    <message-counter-history-day-limit>10</message-counter-history-day-limit>
                </address-setting>
            </address-settings>

            <jms-connection-factories>
                <connection-factory name="InVmConnectionFactory">
                    <connectors>
                        <connector-ref connector-name="in-vm"/>
                    </connectors>
                    <entries>
                        <entry name="java:/ConnectionFactory"/>
                    </entries>
                </connection-factory>
                <connection-factory name="RemoteConnectionFactory">
                    <connectors>
                        <connector-ref connector-name="netty"/>
                    </connectors>
                    <entries>
                        <entry name="java:jboss/exported/jms/RemoteConnectionFactory"/>
                    </entries>
                </connection-factory>
                <pooled-connection-factory name="hornetq-ra">
                    <transaction mode="xa"/>
                    <connectors>
                        <connector-ref connector-name="in-vm"/>
                    </connectors>
                    <entries>
                        <entry name="java:/JmsXA"/>
                    </entries>
                </pooled-connection-factory>
            </jms-connection-factories>

            <jms-destinations>
                <jms-queue name="ExpiryQueue">
                    <entry name="java:/jms/queue/ExpiryQueue"/>
                </jms-queue>
                <jms-queue name="DLQ">
                    <entry name="java:/jms/queue/DLQ"/>
                </jms-queue>
            </jms-destinations>
        </hornetq-server>
    </subsystem>

Following are socket bindings in same file:

<socket-binding name="messaging" port="5445"/>
    <socket-binding name="messaging-group" port="0" multicast-address="${jboss.messaging.group.address:231.7.7.7}" multicast-port="${jboss.messaging.group.port:9876}"/>
    <socket-binding name="messaging-throughput" port="5455"/>

I tried it like below because unable to see org.jnp.interfaces.NamingContextFactory class in Jboss EAP 6.3:

prop.put(Context.PROVIDER_URL,"localhost:5445"); 
prop.put(Context.INITIAL_CONTEXT_FACTORY,"org.hornetq.core.remoting.impl.netty.NettyConnectorFactory");
prop.put(Context.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");

Currently, it is throwing Connection Exception.

Could anyone please suggest or submit a java program on how to achieve connection with Hornetq in Jboss EAP 6.3?

Update: I still do not know whether i am following correct procedure of doing the same.

Following is exceptions which i am getting:

javax.naming.NamingException: JBAS011843: Failed instantiate InitialContextFactory org.jnp.interfaces.NamingContextFactory from classloader ModuleClassLoader for Module 

I have checked its jboss-client.jar in bin/client and found the above interface is not present there but is present in previous versions which contains jbiss-allclient.jar. I do not think putting that in this jboss version is correct to do.

2

There are 2 answers

0
fatherazrael On BEST ANSWER

Click this jboss link; which contains quickstart program which resolves mentioned problem.

1) Download the code; Import pom.xml of run jboss-helloworld-jms in netbeans. Run clean build.

2) Use this link to resolve maven build problems: Pom not found

3) Following is snippet which used to connect to server

private static final String DEFAULT_MESSAGE = "Hello, World!";
private static final String DEFAULT_CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
private static final String DEFAULT_DESTINATION = "jms/queue/test";
private static final String DEFAULT_MESSAGE_COUNT = "1";
private static final String DEFAULT_USERNAME = "quickstartUser";
private static final String DEFAULT_PASSWORD = "quickstartPwd1!";
private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
private static final String PROVIDER_URL = "remote://localhost:4447";

String connectionFactoryString = System.getProperty("connection.factory", DEFAULT_CONNECTION_FACTORY);
log.info("Attempting to acquire connection factory \"" + connectionFactoryString + "\"");
connectionFactory = (ConnectionFactory) context.lookup(connectionFactoryString);
log.info("Found connection factory \"" + connectionFactoryString + "\" in JNDI"); 

4) Create User "quickstartUser"/"quickstartPwd1!", in Jboss EAP 6.3; using adduser.bat in application realm

5) Do check whether these jms/queue/test and jms/RemoteConnectionFactory exist in Standalone.xml.

6) Run code as simple Java Application and magic is there.

2
Martin Baumgartner On

According to your stacktrace i assume you didn't adept your old JNDI settings to JBoss EAP 6.3

From: http://blog.akquinet.de/2014/09/26/jboss-eap-wildfly-three-ways-to-invoke-remote-ejbs/

Properties prop = new Properties();

prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");

prop.put(Context.PROVIDER_URL, "http-remoting://127.0.0.1:8080");
prop.put(Context.SECURITY_PRINCIPAL, "username");
prop.put(Context.SECURITY_CREDENTIALS, "password");

prop.put("jboss.naming.client.ejb.context", false);
prop.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");

Context context = new InitialContext(prop); 

Your application cant instantiate the initialContext, which is a problem you should focus on.

Also, it looks kinda weird to me that both, the message queue and your jndi is trying to connect localhost:5445.