且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Spring Web连接到嵌入Jboss服务器7.1.1的HornetQ JMS

更新时间:2023-02-22 19:31:23

如果替换,它应该可以工作

It should work, if you replace

<bean name="hornetConnectionFactory" class="org.hornetq.jms.client.HornetQJMSConnectionFactory">
    <constructor-arg name="ha" value="false"></constructor-arg>
    <constructor-arg>
        <bean name="transportConfiguration" class="org.hornetq.api.core.TransportConfiguration">
            <constructor-arg
                value="org.hornetq.core.remoting.impl.netty.NettyConnectorFactory" />
            <constructor-arg>
                <map key-type="java.lang.String" value-type="java.lang.Object">
                    <entry key="host" value="127.0.0.1" />
                    <entry key="port" value="5445" />
                </map>
            </constructor-arg>
        </bean>
    </constructor-arg>
</bean>

<!-- ConnectionFactory Definition -->
<bean id="connectionFactory"
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="hornetConnectionFactory"/>
</bean>

<!-- Definition of the JMS queue -->
<bean id="defaultDestination" class="org.hornetq.jms.client.HornetQQueue">
    <constructor-arg index="0" value="DemoQueue"></constructor-arg>
</bean>

使用

<jee:jndi-lookup id="connectionFactory" jndi-name="java:/JmsXA"/>
<jee:jndi-lookup id="defaultDestination" jndi-name="java:/queue/DemoQueue"/>

这样做,您也不应该在pom中也需要hornetq依赖项,因为使用JNDI查找做到这一点与实现无关(这是一件好事).您参考的说明是关于连接到独立 hornetq服务器的,在这种情况下,您将需要实现.但是,连接到嵌入式程序不需要它们.

Doing that, you shouldn't need hornetq dependencies in your pom either, as doing it with a JNDI lookup is implementation agnostic (which is a good thing). The instructions you refer to are about connecting to standalone hornetq server, and in that case you would need the implementations. However, connecting to embedded should not need them.

当然,假设您的spring应用程序已部署在具有嵌入式HornetQ的JBoss中.

Assuming, of course, that your spring app is deployed within JBoss having the embedded HornetQ.

更新:好的,因此您添加的内容是实际上是从Tomcat连接到JBoss实例.在那种情况下,我只会看到两件事是错误的:

Update: ok, so you added that you're actually connecting from Tomcat to JBoss instance. In that case I only see two things wrong:

<hornetq.version>2.2.18.Final</hornetq.version>

这是错误的,因为它与JBoss 7.1.1随附的版本不匹配,并说明了拓扑错误.应该是:

This is wrong as it doesn't match the version shipped with JBoss 7.1.1, and explains your topology error. It should be:

<hornetq.version>2.2.13.Final</hornetq.version>

此外,在解决此问题后,您会收到一个安全错误,因为您没有禁用安全性,但是也没有向JMS连接提供任何用户凭据.尝试将其添加到您的JBoss HornetQ配置中:

Also, when that has been fixed, you'd get a security error as you haven't disabled the security but haven't provided any user credentials to your JMS connection either. Try adding this to your JBoss HornetQ configuration:

<subsystem xmlns="urn:jboss:domain:messaging:1.1">
    <hornetq-server>
        <security-enabled>false</security-enabled> <!-- <- this part -->

如果不想使用此安全性,请禁用它.

to disable the security, if you don't want to use it.

我也对此进行了测试,包含github中的示例代码.

I also tested this, this contains the example code in github.

Update2:为了能够使用JMS Security,请替换为

Update2: to be able to use JMS Security, replace this

<!-- ConnectionFactory Definition -->
<bean id="connectionFactory"
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="hornetConnectionFactory"/>
</bean>

与此

<!-- ConnectionFactory Definitions -->
<bean id="userCredsConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
   <property name="targetConnectionFactory"><ref bean="hornetConnectionFactory"/></property>
   <property name="username"><value>jmsuser</value></property>
   <property name="password"><value>hornetq</value></property>
   <!-- use credentials of some user you have added in 'jmsrole' group as application
        user in jboss in the above config -->
</bean>

<bean id="connectionFactory"
    class="org.springframework.jms.connection.CachingConnectionFactory">
    <constructor-arg ref="userCredsConnectionFactory"/>
</bean>