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

注: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/Spring Boot整合Weblogic JMS实战的更多相关文章

  1. RabbitMQ与Spring的框架整合之Spring Boot实战

    1.RabbitMQ与Spring的框架整合之Spring Boot实战. 首先创建maven项目的RabbitMQ的消息生产者rabbitmq-springboot-provider项目,配置pom ...

  2. Spring Boot整合实战Spring Security JWT权限鉴权系统

    目前流行的前后端分离让Java程序员可以更加专注的做好后台业务逻辑的功能实现,提供如返回Json格式的数据接口就可以.像以前做项目的安全认证基于 session 的登录拦截,属于后端全栈式的开发的模式 ...

  3. spring boot整合activemq消息中间件

    spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  4. activeMQ入门+spring boot整合activeMQ

    最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ.zeroMQ.rocketMQ.Kafka等后续再去学习 ...

  5. Spring Boot 从入门到实战汇总

    之前写过几篇spring boot入门到实战的博文,因为某些原因没能继续. 框架更新迭代很快,之前还是基于1.x,现在2.x都出来很久了.还是希望能从基于该框架项目开发的整体有一个比较系统的梳理,于是 ...

  6. Spring boot 整合spring Data JPA+Spring Security+Thymeleaf框架(上)

    近期上班太忙所以耽搁了给大家分享实战springboot 框架的使用. 以下是spring boot 整合多个框架的使用. 首先是准备工作要做好. 第一  导入框架所需的包,我们用的事maven 进行 ...

  7. Spring Boot (十三): Spring Boot 整合 RabbitMQ

    1. 前言 RabbitMQ 是一个消息队列,说到消息队列,大家可能多多少少有听过,它主要的功能是用来实现应用服务的异步与解耦,同时也能起到削峰填谷.消息分发的作用. 消息队列在比较主要的一个作用是用 ...

  8. RabbitMQ与Spring的框架整合之Spring Cloud Stream实战

    1.RabbitMQ与Spring Cloud Stream整合实战.SpringCloud Stream整体结构核心概念图,如下所示: 图示解释:Outputs输出,即消息的发送端.Inputs输入 ...

  9. RabbitMQ使用及与spring boot整合

    1.MQ 消息队列(Message Queue,简称MQ)——应用程序和应用程序之间的通信方法 应用:不同进程Process/线程Thread之间通信 比较流行的中间件: ActiveMQ Rabbi ...

随机推荐

  1. android -------- java.net.UnknownServiceException

    最近升级了Android的API版本时 ,导致我的网络请求失败了, 出现了这个错误 java.net.UnknownServiceException, 这个错误,我在网上查到这个主要是由于,我们的Ok ...

  2. Alibaba Java Diagnostic Tool Arthas/Alibaba Java诊断利器Arthas

    Arthas 用户文档 — Arthas 3.1.0 文档https://alibaba.github.io/arthas/ alibaba/arthas: Alibaba Java Diagnost ...

  3. linux系统telnet端口不通能收到SYN但不回SYN+ACK响应问题排查(转载)

    linux系统telnet端口不通能收到SYN但不回SYN+ACK响应问题排查 一:背景:一台机器从公司办公网登录不上且所有tcp端口都telnet不通,但是通过同机房同的其它机器却可以正常访问到出问 ...

  4. pytorch对模型参数初始化

    1.使用apply() 举例说明: Encoder :设计的编码其模型 weights_init(): 用来初始化模型 model.apply():实现初始化 # coding:utf- from t ...

  5. 【linux】Linux 运行进程实时监控pidstat命令详解

    简介 pidstat主要用于监控全部或指定进程占用系统资源的情况,如CPU,内存.设备IO.任务切换.线程等.pidstat首次运行时显示自系统启动开始的各项统计信息,之后运行pidstat将显示自上 ...

  6. Linux下手动查杀木马

    (1).模拟木马程序病原体并让其自动运行 黑客让脚本自动执行的3种方法:1.计划任务:2.开机启动:3.系统命令被人替换,定一个触发事件. 1)生成木马程序病原体 [root@youxi1 ~]# v ...

  7. 改进初学者的PID-采样时间

    最近看到了Brett Beauregard发表的有关PID的系列文章,感觉对于理解PID算法很有帮助,于是将系列文章翻译过来!在自我提高的过程中,也希望对同道中人有所帮助.作者Brett Beaure ...

  8. NETTY option参数

    Channel配置参数 (1).通用参数 CONNECT_TIMEOUT_MILLIS :   Netty参数,连接超时毫秒数,默认值30000毫秒即30秒. MAX_MESSAGES_PER_REA ...

  9. 海思HI2115芯片-NB-IOT模块向外发短信测试

    1. 说是有短信这个功能,测试下怎么使用?先使能BIP功能 AT+NCONFIG=ENABLE_BIP,TRUE 给SIM卡上电 AT+NUICC= 查询下短信中心服务号码 AT+CSCA? 发送短信 ...

  10. Selenium IDE命令

    Selenium IDE中提供了丰富的操作命令,在Selenium IDE的Command的下拉列表框中可以选择使用这些命令. 下面介绍一些常用命令的使用. 1.open open(url) 在浏览器 ...