内容简介:在针对WebLogic 12c运行Spring Boot应用程序时,您有两个选择:1. 初始化InitialContext并从外部连接到WebLogic 12c:要运行它,您只需要将weblogic客户端jar安装为maven依赖项,避免发生“weblogic.jndi.WLInitialContextFactory”的ClassNotFoundException。
在针对WebLogic 12c运行Spring Boot应用程序时,您有两个选择:
- 从外部连接到WebLogic并在Tomcat或其他容器上独立运行Spring Boot应用程序
- 将Spring Boot应用程序直接部署到WebLogic
1. 初始化InitialContext并从外部连接到WebLogic 12c:
@Bean
public InitialContext webLogicInitialContextExt() {
System.out.println("Initializing WL context!");
Hashtable<String, String> h = new Hashtable<String, String>(7);
h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
h.put(Context.SECURITY_PRINCIPAL, "weblogic");
h.put(Context.SECURITY_CREDENTIALS, "weblogic123");
InitialContext ctx = null;
try {
ctx = new InitialContext(h);
} catch (NamingException e) {
e.printStackTrace();
}
return ctx;
}
要运行它,您只需要将weblogic客户端jar安装为maven依赖项,避免发生“weblogic.jndi.WLInitialContextFactory”的ClassNotFoundException。
只需参考您的Oracle WebLogic安装的server / lib文件夹并查找wlthint3client.jar,它是一个最小的jar,包含连接到WLS和获取一些JNDI内容的必要类的最小列表。我强烈建议通过maven安装它以避免头痛问题。
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> \
-DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=<packaging>
2. 在WebLogic 12c中运行的InitialContext和Spring Boot
如果您在WebLogic中运行Spring Boot应用程序,那么初始化Context会更简单:
@Bean
public Context webLogicInitialContextServerSide() throws NamingException{
return new InitialContext();
}
您无需指定工厂或URL。默认情况下,服务器将使用WLInitialContextFactory类及其URL。
使用InitialContext通过JNDI获取服务器工件
让我们创建一个简单的应用程序,它将每秒将JMS消息推送到JMS队列中。
首先请参考服务器端创建的InitialContext :(如果您希望来自外部WebLogic的InitialContext,只需将bean名称更改为“webLogicInitialContextExt”)
@Resource(name =“webLogicInitialContextServerSide”)
public Context ctx;
整个发送类:(适当配置你的JMS):
@Service
public class JMSUtils {
@Resource(name="webLogicInitialContextServerSide")
public Context ctx;
// Defines the JMS context factory.
public final static String JMS_FACTORY="jms.hs.ConnectionFactory";
// Defines the queue.
public final static String QUEUE="jms.hs.Test";
private QueueConnectionFactory qconFactory;
private QueueConnection qcon;
private QueueSession qsession;
private QueueSender qsender;
private Queue queue;
private TextMessage msg;
@Scheduled(fixedRate = 1000)
public void sendIntoJMSQueue() throws Exception{
try {
System.out.println("Initializing...");
init(ctx, QUEUE);
Date currentTime = new Date();
System.out.println(currentTime);
send(currentTime.toString());
} finally {
close();
}
}
public void init(Context ctx, String queueName)
throws NamingException, JMSException
{
qconFactory = (QueueConnectionFactory) ctx.lookup(JMS_FACTORY);
qcon = qconFactory.createQueueConnection();
qsession = qcon.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
queue = (Queue) ctx.lookup(queueName);
qsender = qsession.createSender(queue);
msg = qsession.createTextMessage();
qcon.start();
}
/**
* Sends a message to a JMS queue.
*
* @param message message to be sent
* @exception JMSException if JMS fails to send message due to internal error
*/<font>
<b>public</b> <b>void</b> send(String message) throws JMSException {
msg.setText(message);
qsender.send(msg);
}
</font><font><i>/**
* Closes JMS objects.
* @exception JMSException if JMS fails to close objects due to internal error
*/</i></font><font>
<b>public</b> <b>void</b> close() throws JMSException {
qsender.close();
qsession.close();
qcon.close();
}
}
</font>
在WebLogic 12c上运行Spring Boot应用程序时,几乎没有任何问题
您的应用需要在src / main / webapp / WEB-INF文件夹中包含weblogic.xml描述符。其他地方不起作用。有关其他详细信息,请阅读 Spring-Boot部署到WebLogic 。另外不要忘记设置以下依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>tomcat-embed-el</artifactId>
<groupId>org.apache.tomcat.embed</groupId>
</exclusion>
</exclusions>
</dependency>
有了这个,您将确保嵌入式servlet容器不会干扰将部署war文件的servlet容器。
测试:
- 安装Weblogic 12c
- 使用JMS队列“jms.hs.Test”和JMS连接工厂“jms.hs.ConnectionFactory”在那里创建域
- git clone https://bitbucket.org/tomask79/spring-boot-weblogic-jms.git
- 将WAR从目标文件夹部署到WebLogic并启动它
- 新消息应出现在“jms.hs.Test”队列中。
点击标题看原文!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
互联网+秋叶课堂:我的网课创业
秋叶 / 北京:机械工业出版社 / 2016-3-1 / 39.0
在线教育被很多人视为 “互联网+”创业热潮中的下一个风口,越来越多的老师和创业者选择在线教育创业。本书作者秋叶老师2013年双11正式带一帮小伙伴开始在线教育创业,在短短两年内“从0到1”,累计做到了超500万元的销售业绩,成为国内Office领域在线教育运营最成功的团队之一。在这本书中秋叶老师结合自己的实战经历,向各位完整剖析了两年创业的真实复盘思考,是一本值得在线教育创业同行入手的必读书。 ......一起来看看 《互联网+秋叶课堂:我的网课创业》 这本书的介绍吧!