Spring Boot和WebLogic 12c

栏目: Java · 发布时间: 5年前

内容简介:在针对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”队列中。

点击标题看原文!


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

世界因你不同:李开复自传(纪念版)

世界因你不同:李开复自传(纪念版)

李开复,范海涛 著作 / 中信出版社 / 2015-7-10 / 39.00

编辑推荐 1.李开复唯一一部描写全面生平事迹的传记:《世界因你不同:李开复自传》书中讲述了家庭教育培育的“天才少年”;学校教育塑造的“创新青年”,走入世界顶级大公司,苹果、微软、谷歌等亲历的风云内幕,岁月30载不懈奋斗、追求事业成功的辉煌历程。 2.娓娓道来、字字珠玑、可读性和故事性皆佳。李开复博士是青少年成长成才的励志偶像,年轻家长、学校教师阅读后也能从中得到感悟和启发。 3.......一起来看看 《世界因你不同:李开复自传(纪念版)》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

在线进制转换器
在线进制转换器

各进制数互转换器

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具