Spring整合Weblogic jms实例详解

网友投稿 195 2022-12-23


Spring整合Weblogic jms实例详解

本文主要介绍weblogic jms的配置,包括JMS 服务器和JMS 模块(连接工厂、队列、远程 SAF 上下文、SAF 导入目的地、SAF 错误处理)的配置;并在Spring环境下进行消息的监听及发送;为了更多的使用webloigc jms的功能,发送的队列使用saf配置的远程weblogic jms队列(两边的weblogic版本须一致),当然本地也是可以的。本文中demo所使用的软件环境为:weblogic 10.3.6.0、spring5.1.2.RELEASE

注:saf配置的远程队列只能发送消息,不能监听消息。

1、weblogic jms配置

1.1、配置JMS 服务器

注:需配置持久性存储,没有就创建一个

1.2、配置JMS 模块

下面的功能都是在JMS 模块中配置;连接工厂、队列、远程 SAF 上下文、SAF 导入目的地、SAF 错误处理

这里就不一一截图配置过程了,按页面提示配置就行;配置结果如下

连接工厂需设置jndi,程序里会用到

SAF 远程上下文配置的远程地址及用户名密码信息

SAF 导入目的地配置的远程的队列消息及对应到本地的jndi

SAF 错误处理程序配置错误处理策略属性,选配

队列需设置jndi,程序里会用到

SAF 导入目的地配置的队列消息如下:

点击队列名称:

本地 JNDI 名称程序里会用到,向该jndi发送消息实际会发送到远程的队列里。

2、spring配置

增加applicationContext-jms.xml文件:

xmlns:xsi="http://w3.org/2001/XMLSchema-instancelOHrX" xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"http:// xmlns:tx="http://springframework.org/schema/tx"

xmlns:security="http://springframework.org/schema/security"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/security

http://springframework.org/schema/security/spring-security-4.3.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-4.3.xsd">

weblogic.jndi.WLInitialContextFactory

t3://10.39.196.10:7001

weblogic

weblogic1

xmlns:xsi="http://w3.org/2001/XMLSchema-instancelOHrX" xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"http:// xmlns:tx="http://springframework.org/schema/tx"

xmlns:security="http://springframework.org/schema/security"

xmlns:task="http://springframework.org/schema/task"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.3.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.3.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.3.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/security

http://springframework.org/schema/security/spring-security-4.3.xsd

http://springframework.org/schema/task

http://springframework.org/schema/task/spring-task-4.3.xsd">

weblogic.jndi.WLInitialContextFactory

t3://10.39.196.10:7001

weblogic

weblogic1

jndiTemplate配置weblogic的连接信息

jmsConnectionFactory配置连接工厂

testQueueSend向该队列发送消息,对应上面saf远程目的地里队列的本地jndi名称,

testQueueReceive对该队列进行监听,接受消息

jmsTemplate配置jms的模板

sender发送消息的类,把该类配置为定时任务,定时发送消息;

receiver监听的类

listenerContainer监听容器

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;

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来发送消息。

接受者:

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();

}

}

}

4、测试

1.发送消息

启动程序后,每隔5秒中会往testQueueSend队列(远程队列)中发送一条消息,可到weblogic控制台查看消息.

2.接受消息

在weblogic控制台手工往testQueueReceive队列插入一条消息,程序日志会打印该消息内容。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JAVA内存溢出解决方案图解
下一篇:java乐观锁原理与实现案例分析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~