Spring JMS 整合Weblogic JMS

2024-09-04 03:48
文章标签 java spring 整合 weblogic jms

本文主要是介绍Spring JMS 整合Weblogic JMS,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.1     JMS简介

       JMS的全称是Java Message Service,即Java消息服务。它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息。把它应用到实际的业务需求中的话我们可以在特定的时候利用生产者生成一消息,并进行发送,对应的消费者在接收到对应的消息后去完成对应的业务逻辑。对于消息的传递有两种类型,一种是点对点的,即一个生产者和一个消费者一一对应;另一种是发布/订阅模式,即一个生产者产生消息并进行发送后,可以由多个消费者进行接收。

1.2     Spring整合JMS

       对JMS做了一个简要介绍之后,接下来就讲一下Spring整合JMS的具体过程。JMS只是一个标准,真正在使用它的时候我们需要有它的具体实现,这里我们就使用Weblogic的Weblogic JMS来作为它的实现。首先我们需要搭建一个Spring框架,并引用操作Weblogic JMS所需要的jar包。搭建Spring框架可以参考博客:https://blog.csdn.net/qq_36769100/article/details/70908148     ,Spirng框架依赖jar包下载地址:https://download.csdn.net/download/u013310119/10311191   。操作Weblogic JMS所需要的jar包下载地址为:https://download.csdn.net/download/u013310119/10311218       。搭建完成Spring框架,把Weblogic JMS的jar包导入后,就可以进行,Spring整合Weblogic JMS的开发。

在applicationContext.xml中配置Weblogic 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:jms="http://www.springframework.org/schema/jms"xmlns:jee="http://www.springframework.org/schema/jee"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/jeehttp://www.springframework.org/schema/jee/spring-jee-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"><bean id="weblogicJms" 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.19.22.94:7011  </prop>  <prop key="java.naming.factory.url.pkgs">  weblogic.jndi.factories  </prop>  <prop key="java.naming.security.principal">  <!--用户名 -->weblogic  </prop>  <prop key="java.naming.security.credentials"> <!--密码  -->weblogic123  </prop>  </props>  </property>  </bean>  <!-- Connection factory -->  <bean id="jmsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">  <property name="jndiName" value="gt3.esb.jms.con.ESBConnectionFactory" />  <property name="jndiTemplate" ref="weblogicJms"/>  </bean>  <!-- 用来接受消息的消息队列 -->  <bean id="jmsQueue" class="org.springframework.jndi.JndiObjectFactoryBean">  <property name="jndiName" value="gt3.esb.jms.mdb.BaseQueueAsynMDBean" /> <property name="jndiTemplate" ref="weblogicJms"/> <!-- <delivery-params-overrides><priority>9</priority>优先级,0最小,9最大</delivery-params-overrides> 批量还是实时,0为实时,25,75,100为批量<messaging-performance-preference>0</messaging-performance-preference> --></bean>  <!-- 用来测试消息回复的 --><bean id="responseQueue" class="org.springframework.jndi.JndiObjectFactoryBean">  <property name="jndiName" value="responseQueue" />  <property name="jndiTemplate" ref="weblogicJms"/>  </bean>  <!-- Receiver --> <!--消息消费者 --> <!-- <bean id="jmsReceiver" class="com.inspur.JMS.service.QueueMsgReceiver1">  </bean> --><bean id="jmsReceiverJDBC" class="com.inspur.JDBCTemplate.listener.ConsumerMessageListener">  </bean>  <bean id="jmsReceiverRsponse" class="com.inspur.JMS.service.ConsumerMessageListener">  </bean>  <!-- Message Listener -->  <!-- 在Spring整合JMS的应用中,如果我们要进行本地的事务管理的话非常简单,只需要在定义对应的消息监听容器时指定其sessionTransacted属性为true,如:  <property name="sessionTransacted" value="true"/>    该属性值默认为false,这样JMS在进行消息监听的时候就会进行事务控制,当在接收消息时监听器执行失败时JMS就会对接收到的消息进行回滚--><!--   如果想接收消息和数据库访问处于同一事务中,那么我们就可以配置一个外部的事务管理同时配置一个支持外部事务管理的消息监听容器(如DefaultMessageListenerContainer)。要配置这样一个参与分布式事务管理的消息监听容器,我们可以配置一个JtaTransactionManager,当然底层的JMS ConnectionFactory需要能够支持分布式事务管理,并正确地注册我们的JtaTransactionManager。这样消息监听器进行消息接收和对应的数据库访问就会处于同一数据库控制下,当消息接收失败或数据库访问失败都会进行事务回滚操作。当给消息监听容器指定了transactionManager时,消息监听容器将忽略sessionTransacted的值。 --><bean id="listenerContainer"  class="org.springframework.jms.listener.DefaultMessageListenerContainer">  <property name="connectionFactory" ref="jmsConnectionFactory"/>  <property name="destination" ref="jmsQueue"/>  <!-- <property name="messageListener" ref="jmsReceiver"/>   --><property name="messageListener" ref="jmsReceiverRsponse"/>  <!--  <property name="transactionManager" ref="jtaTransactionManager"/>   --><property name="autoStartup" value="true"/>  <!-- 设置固定的线程数 -->  <property name="concurrentConsumers" value="6"></property>  <!-- 	设置动态的线程数   --><property name="concurrency" value="2-9"></property>  <!-- 设置最大的线程数 -->  <property name="maxConcurrentConsumers" value="15"></property> </bean><!--  <bean id="jtaTransactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>  <tx:annotation-driven transaction-manager="jtaTransactionManager"/> --><!-- Spring JMS Template -->  <!--Spring提供了专门的jmsTemplate对消息队列进行操作--><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate" lazy-init="true">  <property name="connectionFactory" ref="jmsConnectionFactory" />  <property name="defaultDestination" ref="jmsQueue" /><property name="receiveTimeout">  <value>5000</value>  </property>     </bean>  <!-- Sender -->  <!--生产者--><bean id="jmsSender" class="com.inspur.JMS.controller.SendJMSMessage"  lazy-init="true">  <property name="jmsTemplate" ref="jmsTemplate"></property>  </bean>  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean><!-- 数据源配置 --><!-- <jee:jndi-lookup jndi-name="JDBC Data Source-0" id="dataSource"/> --><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"       destroy-method="close">      <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />      <property name="url" value="jdbc:oracle:thin:@10.19.22.91:1521:orcl" />      <property name="username" value="ydpt" />      <property name="password" value="ydpt" />      </bean> <bean id="Student" class="entity.student">  <property name="name">  <value>Tom</value>  </property>  </bean>  <context:component-scan base-package="com.inspur" ><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 配置spring注解扫描的包 -->    <context:component-scan base-package="com.elgin"></context:component-scan>    <!-- 或者不用注解的形式加载bean,改用配置的方式    <bean id="springWebService" class="com.elgin.spring.webservice.SpringWebServiceDemo ">      利用property可以对SpringService类进行初始化,比如<property name="name" value="姚明" /><property name="job" value="职业男篮" />,在配置完SpringService类后,就可以直接在程序中FileSystemXmlApplicationContext类或其他类似功能的类读取applicationContext.xml文件中的内容,并获得SpringService类的对象实例。但现在我们并不这样做,而是将SpringService类发布成WebService。在Tomcat的webapps项目中的WEB-INF\lib目录中有一个axis2-spring-1.4.1.jar文件, 该文件用于将被装配JavaBean的发布成WebService。  -->    </beans>

    接着我们来测试一下,看看我们的整合是否真的成功了,测试代码如下:

@Controller
@RequestMapping("test")
public class TestController {@Autowired@Qualifier("jmsQueue")private Destination destination;@Autowiredprivate ProducerServiceImpl producerService;@RequestMapping("first")public  String first() {producerService.sendMessage(destination, "你好,现在是:" + new Date().toLocaleString());return null;}}
@Component
public class ProducerServiceImpl  {@Autowiredprivate JmsTemplate jmsTemplate;public void sendMessage(Destination destination, final String message) {System.out.println("---------------生产者发送消息-----------------");System.out.println("---------------生产者发了一个消息:" + message);jmsTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {/*TextMessage textMessage = session.createTextMessage(message);textMessage.setJMSReplyTo(responseDestination);return textMessage;*/return session.createTextMessage(message);/*MapMessage message = session.createMapMessage();message.setString("name1", "value1");message.setString("name2", "value2");message.setString("name3", "value3");return message;*/}});}public void sendMessage(final Destination destination, final Serializable obj) {//未使用MessageConverter的情况/*jmsTemplate.send(destination, new MessageCreator() {public Message createMessage(Session session) throws JMSException {ObjectMessage objMessage = session.createObjectMessage(obj);return objMessage;}});*///使用MessageConverter的情况jmsTemplate.convertAndSend(destination, obj);jmsTemplate.execute(new SessionCallback<Object>() {public Object doInJms(Session session) throws JMSException {MessageProducer producer = session.createProducer(destination);Message message = session.createObjectMessage(obj);producer.send(message);return null;}});jmsTemplate.execute(new ProducerCallback<Object>() {public Object doInJms(Session session, MessageProducer producer)throws JMSException {Message message = session.createObjectMessage(obj);producer.send(destination, message);return null;}});}}

消费者

public class ConsumerMessageListener implements MessageListener {


@Autowired
private TestDao testDao;

private int count = 0;

public void onMessage(Message message) {
//这里我们知道生产者发送的就是一个纯文本消息,所以这里可以直接进行强制转换,或者直接把onMessage方法的参数改成Message的子类TextMessage

System.out.println("在onMessage中线程ID是"+Thread.currentThread());  
TextMessage textMsg = (TextMessage) message;



System.out.println(new Date().toLocaleString() + "接收到一个纯文本消息。");
try {
String text = textMsg.getText();
System.out.println("消息内容是:" + text);

} catch (JMSException e) {
e.printStackTrace();
}
}


}

测试结果如下:


下面提供demo源码下载地址:

https://download.csdn.net/download/u013310119/10311333


这篇关于Spring JMS 整合Weblogic JMS的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1134915

相关文章

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

Java controller接口出入参时间序列化转换操作方法(两种)

《Javacontroller接口出入参时间序列化转换操作方法(两种)》:本文主要介绍Javacontroller接口出入参时间序列化转换操作方法,本文给大家列举两种简单方法,感兴趣的朋友一起看... 目录方式一、使用注解方式二、统一配置场景:在controller编写的接口,在前后端交互过程中一般都会涉及

Java中的StringBuilder之如何高效构建字符串

《Java中的StringBuilder之如何高效构建字符串》本文将深入浅出地介绍StringBuilder的使用方法、性能优势以及相关字符串处理技术,结合代码示例帮助读者更好地理解和应用,希望对大家... 目录关键点什么是 StringBuilder?为什么需要 StringBuilder?如何使用 St

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格

Java并发编程之如何优雅关闭钩子Shutdown Hook

《Java并发编程之如何优雅关闭钩子ShutdownHook》这篇文章主要为大家详细介绍了Java如何实现优雅关闭钩子ShutdownHook,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起... 目录关闭钩子简介关闭钩子应用场景数据库连接实战演示使用关闭钩子的注意事项开源框架中的关闭钩子机制1.

Maven中引入 springboot 相关依赖的方式(最新推荐)

《Maven中引入springboot相关依赖的方式(最新推荐)》:本文主要介绍Maven中引入springboot相关依赖的方式(最新推荐),本文给大家介绍的非常详细,对大家的学习或工作具有... 目录Maven中引入 springboot 相关依赖的方式1. 不使用版本管理(不推荐)2、使用版本管理(推

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll