mvn install:install-file -Dfile=myArtifact.jar -DgroupId=x.y.z -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar -DgeneratePom=true
首先需要引入weblogic客户端jar包:
weblogic
wlclient
1.0.0
wlclient.jar在maven中央仓库是没有资源的,需要手动下载wlclient的jar,然后通过maven命令打入到本地仓库。mvn安装jar到本地仓库的命令为:
mvn install:install-file -Dfile=myArtifact.jar -DgroupId=x.y.z -DartifactId=${artifactId} -Dversion=${version} -Dpackaging=jar -DgeneratePom=true |
weblogic
wlclient
1.0.0
${basedir}/path/to/library/wlclient-1.0.0.jar
system
package com.sgm.ici.personalization.ms;
import java.util.Properties;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
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.sgm.ici.personalization.ms.service.util.MyMessageListener;
@Configuration
public class JmsConfiguration{
private StringuserName ="cmat";
private StringconnectionFactory ="jms/IDMTopicConnectionFactory";
private Stringpassword ="Pass1234";
private Stringtopic ="jms/IDMICITopic";
private Stringurl ="t3://10.203.25.58:7003";
@Autowired
private MyMessageListenermyMessageListener;
@Bean(name ="weblogicJms")
public JndiTemplate weblogicJms(){
Properties props =new Properties();
props.setProperty("java.naming.factory.initial","weblogic.jndi.WLInitialContextFactory");
props.setProperty("java.naming.provider.url",url );
props.setProperty("java.naming.security.principal",userName);
props.setProperty("java.naming.security.credentials",password);
JndiTemplate jmsJndiTemplate =new JndiTemplate();
jmsJndiTemplate.setEnvironment(props);
returnjmsJndiTemplate;
}
@Bean(name ="jmsConnectionFactory")
public JndiObjectFactoryBean jmsConnectionFactory(){
JndiObjectFactoryBean jndiObjectFactoryBean =new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName(connectionFactory);
jndiObjectFactoryBean.setJndiTemplate(weblogicJms());
returnjndiObjectFactoryBean;
}
@Bean(name ="jmsDestination")
public JndiObjectFactoryBean jmsDestination(){
JndiObjectFactoryBean jmsDestination =new JndiObjectFactoryBean();
jmsDestination.setJndiName(topic);
jmsDestination.setJndiTemplate(weblogicJms());
returnjmsDestination;
}
@Bean(name ="listenerTopic")
@ConditionalOnMissingBean
public DefaultMessageListenerContainer listenerTopic(){
DefaultMessageListenerContainer listener = new DefaultMessageListenerContainer();
listener.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject());
listener.setDestination((Destination) jmsDestination().getObject());
listener.setAutoStartup(true);
listener.setPubSubDomain(true);
listener.setMessageListener(myMessageListener);
returnlistener;
}
@Bean(name ="jmsTemplate" )
@ConditionalOnMissingBean
public JmsTemplate getJmsTemplate(){
JmsTemplate jmsTemplate =new JmsTemplate();
jmsTemplate.setConnectionFactory((ConnectionFactory) jmsConnectionFactory().getObject());
jmsTemplate.setDefaultDestination((Destination) jmsDestination().getObject());
jmsTemplate.setPubSubDomain(true);
jmsTemplate.setExplicitQosEnabled(true);
returnjmsTemplate;
}
}
因为我的项目中会默认初始化一个JmsTemplate 所以我这里加了@ConditionalOnMissingBean表示在当前上下文中不存在某个对象时,才会实例化一个Bean,不然会导致spring容器中存在两个相同类型的bean而启动报错。我这里采用的订阅topic的模式,当然也可以监听一个queue,
这里我就不做多个解释,其实是一模一样的配置。
MyMessageListener 类异步监听topic内的消息,如果有消息进来会自动消费:
package com.sgm.ici.personalization.ms.service.util;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component
public class MyMessageListenerimplements MessageListener{
@Override
public void onMessage(Message message) {
if (messageinstanceof TextMessage) {
try {
String msg = ((TextMessage)message).getText();
System.out.println("ok--"+msg);
//JSONObject.parseObject("",clazz);
} catch (JMSExceptione) {
thrownew RuntimeException(e);
}
} else {
thrownew IllegalArgumentException(
"Message must be of type TestMessage");
}
}
}
测试JMS类 :
package com.sgm.ici.personalization.ms.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping(value = "/")
public class JmsController {
@Autowired
@Qualifier("jmsTemplate")
private JmsTemplatejmsTemplate;
@RequestMapping(value ="/sendMsg", method = RequestMethod.GET)
@ResponseBody
public Object loginOut(HttpServletRequestreq, HttpServletResponse resp){
jmsTemplate.convertAndSend("hello world " +System.currentTimeMillis() );
returnnull;
}
}
所有springboot的dependencies:
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<java.version>1.8java.version>
<ici.version>0.0.1-SNAPSHOTici.version>
<ojdbc6.version>11.2.0.1.0ojdbc6.version>
<druid.version>1.0.24druid.version>
<mybatis-spring.version>1.2.0mybatis-spring.version>
<swagger.version>2.6.1swagger.version>
<pagehelper.version>1.1.2pagehelper.version>
<orderby-helper.version>0.0.2orderby-helper.version>
<mapper-spring-boot.version>1.1.0mapper-spring-boot.version>
<cglib.version>2.2cglib.version>
<log4j.version>2.6.2log4j.version>
<mockito.version>2.6.2mockito.version>
<auth.center.version>1.0.0-SNAPSHOTauth.center.version>
<poi.version>3.17poi.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<exclusions>
<exclusion>
<artifactId>logback-classicartifactId>
<groupId>ch.qos.logbackgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-aopartifactId>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc6artifactId>
<version>${ojdbc6.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>${mybatis-spring.version}version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>${swagger.version}version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>${swagger.version}version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>${pagehelper.version}version>
<exclusions>
<exclusion>
<artifactId>mybatis-spring-boot-starterartifactId>
<groupId>org.mybatis.spring.bootgroupId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>tk.mybatisgroupId>
<artifactId>mapper-spring-boot-starterartifactId>
<version>${mapper-spring-boot.version}version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>${druid.version}version>
dependency>
<dependency>
<groupId>cglibgroupId>
<artifactId>cglib-nodepartifactId>
<version>${cglib.version}version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>org.mockitogroupId>
<artifactId>mockito-coreartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.4version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jmsartifactId>
dependency>
<dependency>
<groupId>javax.jmsgroupId>
<artifactId>jmsartifactId>
<version>1.1version>
dependency>
<dependency>
<groupId>weblogicgroupId>
<artifactId>wlthint3clientartifactId>
<version>10.3.6.0version>
dependency>
<dependency>
<groupId>com.ctrip.framework.apollogroupId>
<artifactId>apollo-clientartifactId>
<version>0.9.0version>
dependency>
dependencies>