编写Java程序以连接到Jboss EAP 6.3中的HornetQ Messaging Service?

时间:2015-06-16 07:12:16

标签: java jboss jms hornetq

我使用的是Jboss EAP 6.3,需要使用Messaging工具。 我曾在Jboss 4x工作,我们可以使用以下代码轻松建立连接:

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

以上方式是否可以在EAP 6.3中连接到HornetQ?如果是,还需要哪些其他配置?另外,我发现在standalone.xml中默认情况下也没有配置1099。

以下是在Standalone-full.xml文件中为HornetQ执行的默认设置:

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

以下是同一文件中的套接字绑定:

<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"/>

我在下面尝试过,因为无法在Jboss EAP 6.3中看到org.jnp.interfaces.NamingContextFactory类:

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

目前,它正在抛出连接异常。

有人可以建议或提交一个关于如何在Jboss EAP 6.3中实现与Hornetq连接的java程序吗?

更新: 我仍然不知道我是否遵循了做同样的正确程序。

以下是我得到的例外情况:

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

我在bin / client中检查了它的jboss-client.jar,发现上面的接口不存在,但是在以前的版本中包含jbiss-allclient.jar。我不认为把它放在这个jboss版本中是正确的。

2 个答案:

答案 0 :(得分:0)

根据您的堆栈跟踪,我假设您没有将旧的JNDI设置擅长JBoss EAP 6.3

来自: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); 

您的应用程序无法实例化initialContext,这是您应该关注的问题。

另外,对我来说,消息队列和你的jndi都试图连接localhost:5445看起来有点奇怪。

答案 1 :(得分:0)

点击this jboss链接;其中包含解决上述问题的快速启动程序。

1)下载代码;在netbeans中导入run jboss-helloworld-jms的pom.xml。运行干净的构建。

2)使用此链接解决maven构建问题: Pom not found

3)以下是用于连接服务器的片段

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)在Jboss EAP 6.3中创建用户&#34; quickstartUser&#34; /&#34; quickstartPwd1!&#34;,在应用领域中使用adduser.bat

5)检查Standalone.xml中是否存在这些jms / queue / test和jms / RemoteConnectionFactory。

6)运行代码作为简单的Java应用程序,魔术就在那里。