Spring Boot和WebLogic 12c


在针对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
     */

    public void send(String message) throws JMSException {
        msg.setText(message);
        qsender.send(msg);
    }

   
/**
     * Closes JMS objects.
     * @exception JMSException if JMS fails to close objects due to internal error
     */

    public void close() throws JMSException {
        qsender.close();
        qsession.close();
        qcon.close();
    }
}

在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”队列中。

点击标题看原文!