Spring整合Weblogic jms实战
Posted 咏吟
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring整合Weblogic jms实战相关的知识,希望对你有一定的参考价值。
本文主要介绍weblogic jms的配置,包括JMS 服务器和JMS 模块(连接工厂、队列、远程 SAF 上下文、SAF 导入目的地、SAF 错误处理)的配置;并在Spring/Spring Boot环境下进行消息的监听及发送;发送的队列使用saf配置的远程weblogic jms队列(两边的weblogic版本须一致),当然本地也是可以的。文中所使用到的软件版本:Weblogic 10.3.6.0、Spring 5.1.2.RELEASE/Spring Boot 2.1.4.RELEASE、Java 1.8.0_181
注:saf配置的远程队列只能发送消息,不能监听消息。
1、Weblogic JMS配置
1.1、配置JMS 服务器
注:需配置持久性存储,没有就创建一个
1.2、配置JMS 模块
下面的功能都是在JMS 模块中配置:连接工厂、队列、远程 SAF 上下文、SAF 导入目的地、SAF 错误处理
这里就不一一截图配置过程了,按页面提示配置就行;配置结果如下
连接工厂需设置jndi,程序里会用到
SAF 远程上下文配置的远程地址及用户名密码信息
SAF 导入目的地配置的远程的队列消息及对应到本地的jndi
SAF 错误处理程序配置错误处理策略属性,选配
队列需设置jndi,程序里会用到
SAF 导入目的地配置的队列消息如下:
点击队列名称:
本地 JNDI 名称程序里会用到,向该jndi发送消息实际会发送到远程的队列里。
2、编写程序
2.1、Spring程序
2.1.1、applicationContext-jms.xml
增加jms的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:security="http://www.springframework.org/schema/security" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-4.3.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.3.xsd"> <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop> <prop key="java.naming.provider.url">t3://10.39.196.10:7001</prop> <prop key="java.naming.security.principal">weblogic</prop> <prop key="java.naming.security.credentials">weblogic1</prop> </props> </property> </bean> <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiTemplate" ref="jndiTemplate" /> <property name="proxyInterface" value="javax.jms.ConnectionFactory" /> <property name="jndiName" value="ConnectionFactory-test" /> </bean> <bean id="testQueueSend" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="testQueueSend" /> <property name="jndiTemplate" ref="jndiTemplate" /> </bean> <bean id="testQueueReceive" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="testQueue" /> <property name="jndiTemplate" ref="jndiTemplate" /> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="jmsConnectionFactory" /> </bean> <bean id="sender" class="com.inspur.demo.jms.Sender"> </bean> <task:scheduled-tasks> <task:scheduled ref="sender" method="hello" cron="0/5 * * * * ?" /> </task:scheduled-tasks> <bean id="receiver" class="com.inspur.demo.jms.Receiver"> </bean> <bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="jmsConnectionFactory" /> <property name="destination" ref="testQueueReceive" /> <property name="messageListener" ref="receiver" /> <property name="autoStartup" value="true" /> </bean> </beans>
jndiTemplate配置weblogic的连接信息
jmsConnectionFactory配置连接工厂
testQueueSend向该队列发送消息,对应上面saf远程目的地里队列的本地jndi名称,
testQueueReceive对该队列进行监听,接受消息
jmsTemplate配置jms的模板
sender发送消息的类,把该类配置为定时任务,定时发送消息;
receiver监听的类
listenerContainer监听容器
2.1.2、发送者
package com.inspur.demo.jms; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; public class Sender { protected static Logger logger = LoggerFactory.getLogger(Sender.class); //发送消息的队列 @Autowired @Qualifier("testQueueSend") private Destination destination; @Autowired private JmsTemplate jmsTemplate; public void hello() { String message = System.currentTimeMillis() + "-hello"; logger.info("Message Send:{}", message); jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } }
使用JmsTemplate来发送消息。
2.1.3、接受者
package com.inspur.demo.jms; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class Receiver implements MessageListener { protected static Logger logger = LoggerFactory.getLogger(Receiver.class); @Override public void onMessage(Message message) { try { String text = ""; if (message instanceof TextMessage) { text = ((TextMessage) message).getText(); } logger.info("Message received:{}", text); } catch (JMSException e) { e.printStackTrace(); } } }
2.2、Spring Boot程序
2.2.1、引入依赖
<dependency> <groupId>javax.jms</groupId> <artifactId>javax.jms-api</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>weblogic</groupId> <artifactId>wlfullclient</artifactId> <version>1.0.0</version> <systemPath>D:\\Oracle\\Middleware\\wlserver_10.3\\server\\lib\\wlfullclient.jar</systemPath> <scope>system</scope> </dependency>
引入的weblogic jar包wlfullclient.jar默认室不存在的,需在D:\\Oracle\\Middleware\\wlserver_10.3\\server\\lib目录下通过命令生成:java -jar wljarbuilder.jar
2.2.2、Weblogic JMS的配置类
package com.inspur.webframe.config; import java.util.Properties; import javax.jms.ConnectionFactory; import javax.jms.Destination; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.listener.DefaultMessageListenerContainer; import org.springframework.jndi.JndiObjectFactoryBean; import org.springframework.jndi.JndiTemplate; import com.inspur.demo.jms.Receiver; @Configuration public class WeblogicJmsConfig { private static Logger logger = LoggerFactory.getLogger(WeblogicJmsConfig.class); @Autowired private Receiver receiver; @Bean public JndiTemplate jndiTemplate() { Properties properties = new Properties(); properties.setProperty("java.naming.factory.initial", "weblogic.jndi.WLInitialContextFactory"); properties.setProperty("java.naming.provider.url", "t3://10.39.196.10:9001"); properties.setProperty("java.naming.security.principal", "weblogic"); properties.setProperty("java.naming.security.credentials", "weblogic1"); JndiTemplate jndiTemplate = new JndiTemplate(); jndiTemplate.setEnvironment(properties); return jndiTemplate; } @Bean public JndiObjectFactoryBean jmsConnectionFactory() { JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("ConnectionFactory-test"); jndiObjectFactoryBean.setJndiTemplate(jndiTemplate()); return jndiObjectFactoryBean; } @Bean("testQueueSend") public JndiObjectFactoryBean testQueueSend() { JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("testQueueSend"); jndiObjectFactoryBean.setJndiTemplate(jndiTemplate()); return jndiObjectFactoryBean; } @Bean("testQueueReceive") public JndiObjectFactoryBean testQueueReceive() { JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean(); jndiObjectFactoryBean.setJndiName("testQueue"); jndiObjectFactoryBean.setJndiTemplate(jndiTemplate()); return jndiObjectFactoryBean; } @Bean("jmsTemplate") @ConditionalOnMissingBean public JmsTemplate jmsTemplate() { JmsTemplate jmsTemplate = new JmsTemplate(); jmsTemplate.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject()); jmsTemplate.setDefaultDestination((Destination) testQueueSend().getObject()); logger.info("jmsTemplate.isExplicitQosEnabled()={}", jmsTemplate.isExplicitQosEnabled()); return jmsTemplate; } @Bean @ConditionalOnMissingBean public DefaultMessageListenerContainer listenerTopic() { DefaultMessageListenerContainer listener = new DefaultMessageListenerContainer(); listener.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject()); listener.setDestination((Destination)testQueueReceive().getObject()); listener.setAutoStartup(true); listener.setMessageListener(receiver); return listener; } }
这里配置的信息与上面applicationContext-jms.xml中配置的内容一致,只不过是通过程序的方式。这里面的一些变动的信息可以配置到application.properties中,如weblogic地址、用户名、密码、队列jndi等。
2.2.3、发送者
package com.inspur.demo.jms; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class Sender { private static Logger logger = LoggerFactory.getLogger(Sender.class); //发送消息的队列 @Autowired @Qualifier("testQueueSend") private Destination destination; @Autowired private JmsTemplate jmsTemplate; @Scheduled(cron = "0/5 * * * * ?") public void hello() { final String message = System.currentTimeMillis() + "-hello"; logger.info("Message Send:{}", message); jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } }
通过注解来定时发送。
2.2.4、接受者
package com.inspur.demo.jms; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; @Component public class Receiver implements MessageListener { protected static Logger logger = LoggerFactory.getLogger(Receiver.class); @Override public void onMessage(Message message) { try { String text = ""; if (message instanceof TextMessage) { text = ((TextMessage) message).getText(); } logger.info("Message received:{}", text); } catch (JMSException e) { e.printStackTrace(); } } }
增加@Component注解,方便配置类中引用。
2.2.5、启动类
在启动类中需增加@EnableAutoConfiguration(exclude = JmxAutoConfiguration.class)注解,否则会报javax.naming.NameNotFoundException: remaining name: env/jmx/runtime异常。原因可能是Spring boot启动时试图创建一个在weblogic api库中检测到的bean(mbeanExporter),这个bean需要env/jmx/runtime JNDI;所以要去除JMX的自动配置。
3、测试
1.发送消息
启动Spring或Spring Boot程序后,每隔5秒中会往testQueueSend队列(远程队列)中发送一条消息,可到Weblogic控制台查看消息.
2.接受消息
在Weblogic控制台手工往testQueueReceive队列插入一条消息,程序日志会打印该消息内容。
以上是关于Spring整合Weblogic jms实战的主要内容,如果未能解决你的问题,请参考以下文章