如何使用JNDI和Spring连接到WildFly 10.1.0.Final ActiveMQ Artemis? [英] How connect to WildFly 10.1.0.Final ActiveMQ Artemis using JNDI and Spring?
问题描述
我有WildFly 10.1.0.Final,这有Artemis 1.1.0 配置jndi.properties的正确方法是什么?:
I have WildFly 10.1.0.Final, this have Artemis 1.1.0 Whats the correct way of configure jndi.properties?:
java.naming.factory.initial=org.apache.activemq.artemis.jndi.ActiveMQInitialContextFactory
java.naming.provider.url=http-remoting://127.0.0.1:8080
jboss.naming.client.ejb.context=false
具有WildFly 9.0.0.Final且具有HornetQ的最终版本的
This worket with WildFly 9.0.0.Final that have HornetQ:
java.naming.factory.initial=org.jboss.naming.remote.client.InitialContextFactory
java.naming.provider.url=http-remoting://127.0.0.1:8080
jboss.naming.client.ejb.context=false
我使用的是Spring 4.3.2,Spring-jms,并在may Maven POM.xml中包含了wildfly-jms-client-bom. 接下来是我的连接工厂. Whith Wildfly 9我尝试了InitialContext.doLookup(..)并完成了这项工作.但是,对于WildFly 10而言,这种方式就无法实现.
I'm using Spring 4.3.2, Spring-jms and included the wildfly-jms-client-bom in may Maven POM.xml. The Next is my connection Factory. Whith Wildfly 9 I try InitialContext.doLookup(..) and this work. But with WildFly 10 this don't work in same way.
我的主要配置文件是:
@Configuration
@ComponentScan(basePackages = "org")
@Import({
MessagingConfiguration.class,
MessagingListenerConfiguration.class
})
public class AppConfig {
//Put Other Application configuration here.
}
.
package org.jms.configuration;
@Configuration
public class MessagingConfiguration {
@Bean
public ConnectionFactory connectionFactory() throws NamingException{
ConnectionFactory connectionFactory = InitialContext.doLookup("java:/ConnectionFactory");
return connectionFactory;
}
/*
@Bean
public JmsTemplate jmsTemplate() throws NamingException {
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(connectionFactory());
//template.setDefaultDestinationName(ORDER_RESPONSE_QUEUE);
return template;
}
*/
}
下一个是我:
package org.jms.configuration;
@Configuration
@EnableJms
public class MessagingListenerConfiguration {
@Autowired
ConnectionFactory connectionFactory;
public MessagingListenerConfiguration() {
System.out.println("MessagingListenerConfiguration::Constructor!!");
}
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setConcurrency("1-1");
return factory;
}
}
最后我有了这个听众.
Finally I have this listener.
package org.jms.messaging;
import javax.jms.JMSException;
import org.gasmart.jms.model.Product;
import org.gasmart.jms.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;
@Component
public class MessageReceiver {
//private static final String ORDER_QUEUE = "order-queue";
/*
* <jms-queue name="shatTopic" entries="java:/jms/queue/shat java:/jboss/exported/jms/queue/shat"/>
*/
private static final String ORDER_QUEUE = "shat";
public MessageReceiver() {
System.out.println(">>>>>>>>>>>>>>>>>>> OK");
}
@Autowired
OrderService orderService;
@JmsListener(destination = ORDER_QUEUE)
public void receiveMessage(final Message<String> message) {
MessageHeaders headers = message.getHeaders();
System.out.println("MessageReceiver::receiveMessage(product) Application : headers received : "+headers);
//orderService.processOrder(product);
}
}
我收到此错误:
20:04:42,971警告[org.springframework.jms.listener.DefaultMessageListenerContainer](DefaultMessageListenerContainer-124)为目标帽子"设置的JMS消息侦听器调用程序失败-试图恢复. 原因:没有名称为sht的队列
20:04:42,971 WARN [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-124) Setup of JMS message listener invoker failed for destination 'shat' - trying to recover. Cause: There is no queue with name shat
20:04:42,971信息[org.springframework.jms.listener.DefaultMessageListenerContainer](DefaultMessageListenerContainer-124)成功刷新了JMS连接
20:04:42,971 INFO [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-124) Successfully refreshed JMS Connection
在MessageReceiver类中声明的 shat 队列. 接下来是我的standalone-full.xml:
The shat queue es declared in MessageReceiver class. My standalone-full.xml is the next:
<subsystem xmlns="urn:jboss:domain:messaging-activemq:1.0">
<server name="default">
<security-setting name="#">
<role name="guest" send="true" consume="true" create-non-durable-queue="true" delete-non-durable-queue="true"/>
</security-setting>
<security-setting name="jms.topic.chat">
<role name="guest2" send="true" consume="true" create-non-durable-queue="true"/>
</security-setting>
<address-setting name="#" dead-letter-address="jms.queue.DLQ" expiry-address="jms.queue.ExpiryQueue" max-size-bytes="10485760" page-size-bytes="2097152" message-counter-history-day-limit="10"/>
<http-connector name="http-connector" socket-binding="http" endpoint="http-acceptor"/>
<http-connector name="http-connector-throughput" socket-binding="http" endpoint="http-acceptor-throughput">
<param name="batch-delay" value="50"/>
</http-connector>
<in-vm-connector name="in-vm" server-id="0"/>
<http-acceptor name="http-acceptor" http-listener="default"/>
<http-acceptor name="http-acceptor-throughput" http-listener="default">
<param name="batch-delay" value="50"/>
<param name="direct-deliver" value="false"/>
</http-acceptor>
<remote-acceptor name="websocket-stomp" socket-binding="netty-ws"/>
<in-vm-acceptor name="in-vm" server-id="0"/>
<jms-queue name="ExpiryQueue" entries="java:/jms/queue/ExpiryQueue"/>
<jms-queue name="DLQ" entries="java:/jms/queue/DLQ"/>
<jms-queue name="shatTopic" entries="java:/jms/queue/shat java:/jboss/exported/jms/queue/shat"/>
<connection-factory name="InVmConnectionFactory" entries="java:/ConnectionFactory" connectors="in-vm"/>
<connection-factory name="RemoteConnectionFactory" entries="java:jboss/exported/jms/RemoteConnectionFactory" connectors="http-connector"/>
<pooled-connection-factory name="activemq-ra" entries="java:/JmsXA java:jboss/DefaultJMSConnectionFactory" connectors="in-vm" transaction="xa"/>
</server>
</subsystem>
我已经定义了正确定义的队列:
I have defined the queue, that is correctly defined:
有关如何声明队列的参考可以在这里进行参考: http: //www.mastertheboss.com/jboss-server/jboss-jms/jboss-jms-configuration
References about how declare a queue can consult here: http://www.mastertheboss.com/jboss-server/jboss-jms/jboss-jms-configuration
为什么我不能连接?我不明白实际上,我尝试了很多方法,没有用.
Why I cant connect?? I don't understand. I actually tryied many many ways, don't work.
更新:2016年8月29日-上午10:00
我发现,如果使用实际部署的应用程序重新启动所有wildfly,则在加载应用程序后,如果我连接了客户端(我有一个Web客户端,该客户端使用Websocket直接将消息生成到"jms.queue.shat"队列中通过带有stomp.js的STOMP)并发送消息,应用程序(java)会正确使用消息.但是,如果我在应用程序正常工作后重新部署它,则会产生错误. 错误是下一个:
I found that if I restart all the wildfly with the application actually deployed, after load the application, If I connect a client (I have a Web client that produce messages directly to the "jms.queue.shat" queue using Websocket over STOMP with stomp.js) and send messages, the application (java) consume correctly the messages. But if I redeploy the application after it was correctly working, this beging to produce ERROR. The errors is next:
10:43:51,607 INFO [org.wildfly.extension.undertow] (ServerService Thread Pool - 113) WFLYUT0021: Registered web context: /ServerApp
10:43:51,632 INFO [org.jboss.as.server] (DeploymentScanner-threads - 2) WFLYSRV0010: Deployed "ServApp.war" (runtime-name : "ServApp.war")
10:43:56,588 INFO [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) JMS message listener invoker needs to establish shared Connection
10:43:56,592 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) Could not refresh JMS Connection for destination 'shat' - retrying using FixedBackOff{interval=5000, currentAttempts=0, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is java.lang.IllegalStateException: Server locator is closed (maybe it was garbage collected)
10:44:01,592 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) Could not refresh JMS Connection for destination 'shat' - retrying using FixedBackOff{interval=5000, currentAttempts=1, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is java.lang.IllegalStateException: Server locator is closed (maybe it was garbage collected)
10:44:06,593 ERROR [org.springframework.jms.listener.DefaultMessageListenerContainer] (DefaultMessageListenerContainer-2) Could not refresh JMS Connection for destination 'shat' - retrying using FixedBackOff{interval=5000, currentAttempts=2, maxAttempts=unlimited}. Cause: Failed to create session factory; nested exception is java.lang.IllegalStateException: Server locator is closed (maybe it was garbage collected)
.....
错误信息: JMS消息侦听器调用程序需要建立共享的连接无法刷新目标'shat'的JMS连接...服务器定位器已关闭(可能已被垃圾回收)
The error say: JMS message listener invoker needs to establish shared Connection Could not refresh JMS Connection for destination 'shat'... Server locator is closed (maybe it was garbage collected)
可能是ActiveMQ Artemis artemis 1.1.0.wildfly-017的错误吗?
Could be a Bug of ActiveMQ Artemis artemis 1.1.0.wildfly-017 ?
有些解决了类似的问题?
Some has resolved some similar?
更新时间:2016年8月29日-下午14:00.
如果有人感兴趣...我发现,如果您使用JndiObjectFactoryBean查找JNDI对象(在本例中为JMS连接工厂),之前的问题将消失.通常,我使用JndiObjectFactoryBean找到了一些示例,但是对于获取数据库数据源,几乎没有获得JMS连接工厂的示例.
If someone is interested... I found that if you use JndiObjectFactoryBean to looks up a JNDI object, in this case the JMS connection factory, the before problem goes away. Normally I found some examples using JndiObjectFactoryBean but for get Database data sources, almost no example to obtain a JMS connection factory.
更改MessagingConfiguration类:
Changing MessagingConfiguration class:
@Bean
public ConnectionFactory connectionFactory() throws NamingException{
ConnectionFactory connectionFactory = InitialContext.doLookup("java:/ConnectionFactory");
return connectionFactory;
}
@Bean
public ConnectionFactory connectionFactory() throws NamingException{
return ConnectionFactory connectionFactory = InitialContext.doLookup("java:/ConnectionFactory");
}
具有:
@Bean
public JndiObjectFactoryBean solicitudesConnectionFactory() {
JndiObjectFactoryBean jndi = new JndiObjectFactoryBean();
jndi.setJndiName("java:/ConnectionFactory");
jndi.setLookupOnStartup(true);
jndi.setProxyInterface(ConnectionFactory.class);
return jndi;
}
public ConnectionFactory notificacionesConnectionFactory() {
return new SingleConnectionFactory((ConnectionFactory)solicitudesConnectionFactory().getObject());
}
与Artemis的连接也可以正常工作,包括如果我重新部署应用程序的话.但是不再使用jndi.properties文件,服务器定位器已关闭"问题消失了.
the connection to Artemis work correctly too, incluse if I do redeploy of the aplication. But the jndi.properties file is not used more, and the "Server locator is closed" problem disappears.
我不知道JndiObjectFactoryBean是否正确,但我想继续使用jndi.properties文件.怎么做?
I don't know if the JndiObjectFactoryBean if correct, but I will want continue using the jndi.properties file. How do it??
推荐答案
尝试将ORDER_QUEUE
从shat
更改为java:/jms/queue/shat
这篇关于如何使用JNDI和Spring连接到WildFly 10.1.0.Final ActiveMQ Artemis?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!