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

相关文章

一文详解如何在idea中快速搭建一个Spring Boot项目

《一文详解如何在idea中快速搭建一个SpringBoot项目》IntelliJIDEA作为Java开发者的‌首选IDE‌,深度集成SpringBoot支持,可一键生成项目骨架、智能配置依赖,这篇文... 目录前言1、创建项目名称2、勾选需要的依赖3、在setting中检查maven4、编写数据源5、开启热

Java对异常的认识与异常的处理小结

《Java对异常的认识与异常的处理小结》Java程序在运行时可能出现的错误或非正常情况称为异常,下面给大家介绍Java对异常的认识与异常的处理,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参... 目录一、认识异常与异常类型。二、异常的处理三、总结 一、认识异常与异常类型。(1)简单定义-什么是

SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志

《SpringBoot项目配置logback-spring.xml屏蔽特定路径的日志》在SpringBoot项目中,使用logback-spring.xml配置屏蔽特定路径的日志有两种常用方式,文中的... 目录方案一:基础配置(直接关闭目标路径日志)方案二:结合 Spring Profile 按环境屏蔽关

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

java中long的一些常见用法

《java中long的一些常见用法》在Java中,long是一种基本数据类型,用于表示长整型数值,接下来通过本文给大家介绍java中long的一些常见用法,感兴趣的朋友一起看看吧... 在Java中,long是一种基本数据类型,用于表示长整型数值。它的取值范围比int更大,从-922337203685477

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程

《SpringBoot集成LiteFlow实现轻量级工作流引擎的详细过程》LiteFlow是一款专注于逻辑驱动流程编排的轻量级框架,它以组件化方式快速构建和执行业务流程,有效解耦复杂业务逻辑,下面给大... 目录一、基础概念1.1 组件(Component)1.2 规则(Rule)1.3 上下文(Conte

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Springboot整合Redis主从实践

《Springboot整合Redis主从实践》:本文主要介绍Springboot整合Redis主从的实例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言原配置现配置测试LettuceConnectionFactory.setShareNativeConnect