Spring Boot:使用Java Bean类从application.properties文件读取spring.jms.jndi-name属性 [英] Spring Boot: Reading spring.jms.jndi-name property from application.properties file using a Java Bean class
问题描述
我正在使用 JNDI查找来获取tomcat服务器上的 Websphere MQ Broker的连接对象.我正在使用 JmsTemplate 将消息发送到WMQ Broker上的队列,并尝试避免基于Spring Xml的配置,因此,我已经配置了Spring Boot的application.properties文件以指定 JNDI查找名称.下面是application.properties文件中的属性.
I am using JNDI lookup for the getting the connection object for Websphere MQ Broker on tomcat server. I am using JmsTemplate for sending the messages to the Queue on WMQ Broker and trying to avoid the Spring Xml based configuration and for that reason i have configured the Spring boot's application.properties file to specify the JNDI look up name.below is the property from application.properties file.
spring.jms.jndi-name= java:comp/env/XXXX
我正在使用Spring bean定义JmsTemplate,下面是它的代码.
i am using a Spring bean to define the JmsTemplate and below is the code for it.
@Configuration
public class JmsMessageTemplateBean {
//@Value("${spring.jms.jndi-name}")
//private ConnectionFactory connectionFactory;
@Bean
public JmsTemplate jmsTemplate() throws Exception{
JmsTemplate jmsMessagingTemplate = new JmsTemplate();
jmsMessagingTemplate.setDefaultDestinationName("Some Queue");
jmsMessagingTemplate.setConnectionFactory(connectionFactory);
return jmsMessagingTemplate;
}
}
我有几个问题:
1.如何从application.properties文件中读取JNDI属性,以及如何在上述bean中将Connection对象设置为Jms Template.
1.How to read the JNDI property from application.properties file and set the Connection object to Jms Template in the above bean.
2.我观察到 JNDI查找中的连接对象是MQQueueConnectionFactory
,而根据我研究的JmsTemplate
支持javax.jms.ConnectionFactory
对象.有没有一种方法可以将MQQueueConnectionfactory
对象转换为javax.jms.Connectionfactory
.
2.I have observed that the connection object from the JNDI lookup is MQQueueConnectionFactory
and from what i have researched JmsTemplate
supports javax.jms.ConnectionFactory
object. is there a way to convert the MQQueueConnectionfactory
object to javax.jms.Connectionfactory
.
欣赏您的答案.
推荐答案
我也很难弄清楚如何实现Spring Boot JMS侦听器,侦听JBoss应用服务器中的ActiveMQ队列.
I also had a hard time figuring out how to implement a Spring Boot JMS Listener, listening to an ActiveMQ queue within a JBoss application server.
ActiveMQ,但由于它位于JBoss服务器内部,因此Spring Boot无法连接ActiveMQ.
实际上,您需要通过在JNDI提供程序上进行查找来自己定义connectionFactory
和jmsListenerContainerFactory
Bean.
ActiveMQ is supported by Spring Boot autoconfiguration, but since it was inside the JBoss server Spring Boot was failing to connect ActiveMQ.
In fact you need to define connectionFactory
and jmsListenerContainerFactory
beans yourself by doing a lookup on the JNDI provider.
@Configuration
@EnableJms
public class ActiveMqConnectionFactoryConfig {
@Value("${broker.url}")
String brokerUrl;
@Value("${borker.username}")
String userName;
@Value("${borker.password}")
String password;
@Value("${queue}")
String queueName;
private static final String INITIAL_CONTEXT_FACTORY = "org.jboss.naming.remote.client.InitialContextFactory";
private static final String CONNECTION_FACTORY = "jms/RemoteConnectionFactory";
@Bean
public ConnectionFactory connectionFactory() {
try {
System.out.println("Retrieving JMS queue with JNDI name: " + CONNECTION_FACTORY);
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName(CONNECTION_FACTORY);
jndiObjectFactoryBean.setJndiEnvironment(getEnvProperties());
jndiObjectFactoryBean.afterPropertiesSet();
return (QueueConnectionFactory) jndiObjectFactoryBean.getObject();
} catch (NamingException e) {
System.out.println("Error while retrieving JMS queue with JNDI name: [" + CONNECTION_FACTORY + "]");
} catch (Exception ex) {
System.out.println("Error");
}
return null;
}
Properties getEnvProperties() {
Properties env = new Properties();
env.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
env.put(Context.PROVIDER_URL, brokerUrl);
env.put(Context.SECURITY_PRINCIPAL, userName);
env.put(Context.SECURITY_CREDENTIALS, password);
return env;
}
@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory(ConnectionFactory connectionFactory) {
DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
JndiDestinationResolver jndiDestinationResolver = new JndiDestinationResolver();
jndiDestinationResolver.setJndiEnvironment(getEnvProperties());
factory.setDestinationResolver(jndiDestinationResolver);
return factory;
}
然后,如果要使用队列,只需使用带有@JmsListener(destination = "${queue}")
Then if you want to consume the queue you just define your JMS consumer class with a method annotated with @JmsListener(destination = "${queue}")
@JmsListener(destination = "${queue}")
public void receive(Message message) {
System.out.println("Received Message: " + message);
}
希望可以节省几个小时的研究;) 干杯
Hope that helps save a few hours of research ;) Cheers
这篇关于Spring Boot:使用Java Bean类从application.properties文件读取spring.jms.jndi-name属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!