JMS生产者的最小代码

时间:2017-06-13 06:07:59

标签: java jms weblogic

我真的很难为JMS生产者获得最低限度的代码,我已经启动并运行了我的WL JMS服务器并准备使用JMS客户端测试它但是为了创建客户端我遇到了使用初始上下文工厂即env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");。我已经尝试了几乎所有的代码,使用WL初始上下文工厂,oracle j2ee初始上下文工厂(env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");),但是当我运行代码时,我总是得不到类定义发现异常。例如,对于下面的代码,我没有发现类定义异常。

我正在尝试将客户端作为独立的Java程序运行。我理解异常,并尝试添加相关的JAR,例如当我使用WL初始上下文工厂然后我将WL完整客户端JAR放在类路径中但仍然无法克服这个没有类def发现的异常。

有人可以链接到某个存储库或博客,或者为我提供一个最小的“工作”JMS制作人,或指出我在做什么错误。

请注意我的JMS服务器是WL,我正在尝试创建一个简单的JMS客户端,而不使用ActiveMQ库,但我也在标记ActiveMQ以便它可以得到更多的关注但是如果有人认为它是错的那么请告诉我,我会删除ActiveMQ标签,或者请随意删除。

我的客户示例:

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class JmsProducerQueueClient {
    public static void main(String[] args) throws NamingException, JMSException {
        Connection connection = null;
        try {
            System.out.println("Create JNDI Context");
            Context context = getInitialContext();

            System.out.println("Get connection facory");
            ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("/com/jms/ms1/cf1");

            System.out.println("Create connection");
            connection = connectionFactory.createConnection();

            System.out.println("Create session");
            Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);

            System.out.println("Lookup queue");
            Queue queue = (Queue) context.lookup("/com/jms/ms1/q1");

            System.out.println("Start connection");
            connection.start();

            System.out.println("Create producer");
            MessageProducer producer = session.createProducer(queue);

            System.out.println("Create hello world message");
            Message hellowWorldText = session.createTextMessage("Hello World!");

            System.out.println("Send hello world message");

            producer.send(hellowWorldText);
        } finally {
            if (connection != null) {
                System.out.println("close the connection");
                connection.close();
            }
        }

    }

    public static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        //env.put(Context.INITIAL_CONTEXT_FACTORY, "oracle.j2ee.rmi.RMIInitialContextFactory");
        env.put(Context.PROVIDER_URL, "tcp://localhost:6007");
        Context context = new InitialContext(env);
        return context;
    }
}

日志:

Create JNDI Context
Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/security/service/PrivilegedActions
    at weblogic.jndi.WLSJNDIEnvironmentImpl.<clinit>(WLSJNDIEnvironmentImpl.java:57)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:190)
    at weblogic.jndi.internal.JNDIEnvironment.getJNDIEnvironment(JNDIEnvironment.java:37)
    at weblogic.jndi.Environment.<clinit>(Environment.java:92)
    at weblogic.jndi.WLInitialContextFactory.getInitialContext(WLInitialContextFactory.java:117)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:684)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.init(InitialContext.java:242)
    at javax.naming.InitialContext.<init>(InitialContext.java:216)
    at com.learning.so.question.jms.queue.producerconsumer.JmsProducerQueueClient.getInitialContext(JmsProducerQueueClient.java:62)
    at com.learning.so.question.jms.queue.producerconsumer.JmsProducerQueueClient.main(JmsProducerQueueClient.java:22)

2 个答案:

答案 0 :(得分:1)

您的代码看起来不错,但您目前在类路径中缺少一个类:  java.lang.NoClassDefFoundError:weblogic / security / service / PrivilegedActions

点击此处,这可能有答案:Weblogic 12.1.3 PrivilegedActions class not found

答案 1 :(得分:1)

我设法使你的代码在我这边工作,所以我认为你的问题来自缺少的依赖,非常肯定来自weblgogic客户端。可能是您打包应用程序的方式就是问题。

所以我将揭露我所做的事情,只使用我自己的队列名称和工厂名称。

请注意,此测试已在 WLS 12.1.3。

上完成

Weblogic客户端

首先,我使用wlthint3client.jar命令在${ORACLE_HOME}/wlserver/server/lib的weblogic服务器上创建了mvn install:install-file的maven依赖关系:

mvn install:install-file -Dfile = wlthint3client.jar -DgroupId = weblogic -DartifactId = wlthint3client -Dversion = 12.1.3.0.0 -Dpackaging = jar

有了这个,我现在可以在我所有的maven项目中使用以下依赖项:

<dependency>
  <groupId>weblogic</groupId>
  <artifactId>wlthint3client</artifactId>
  <version>12.1.3.0.0</version>
</dependency>

Weblogic JMS Factory和队列

然后我在weblogic端创建了一个JMSXAFactory工厂和一个test JMS队列。

JMS Producer项目

然后我创建了maven项目。这是pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>org.jvi.weblogic.jms</groupId>
  <artifactId>jms-producer</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>jms-producer</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>

    <dependency>
      <groupId>weblogic</groupId>
      <artifactId>wlthint3client</artifactId>
      <version>12.1.3.0.0</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <includeScope>runtime</includeScope>
                            <outputDirectory>${project.build.outputDirectory}</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

App类代码:

package org.jvi.weblogic.jms;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Hello world!
 *
 */
public class App 
{
    public final static String JNDI_FACTORY = "weblogic.jndi.WLInitialContextFactory";

    public final static String  URL = "t3://localhost:7001";

    public static void main( String[] args ) throws NamingException, JMSException
    {
        System.out.println( "Hello World!" );

        Connection connection = null;
        try {
            System.out.println("Create JNDI Context");
            Context context = getInitialContext();

            System.out.println("Get connection facory");
            ConnectionFactory connectionFactory = (ConnectionFactory) context.lookup("JMSXAFactory");

            System.out.println("Create connection");
            connection = connectionFactory.createConnection();

            System.out.println("Create session");
            Session session = connection.createSession(false, QueueSession.AUTO_ACKNOWLEDGE);

            System.out.println("Lookup queue");
            Queue queue = (Queue) context.lookup("test");

            System.out.println("Start connection");
            connection.start();

            System.out.println("Create producer");
            MessageProducer producer = session.createProducer(queue);

            System.out.println("Create hello world message");
            Message hellowWorldText = session.createTextMessage("Hello World!");

            System.out.println("Send hello world message");

            producer.send(hellowWorldText);
        } finally {
            if (connection != null) {
                System.out.println("close the connection");
                connection.close();
            }
        }
    }

    private static InitialContext getInitialContext() throws NamingException
            {
              Hashtable env = new Hashtable();
              env.put(Context.INITIAL_CONTEXT_FACTORY, JNDI_FACTORY);
              env.put(Context.PROVIDER_URL, URL);
              return new InitialContext(env);
            }
}

然后当我通过命令行启动应用程序时:

java -cp jms-producer-0.0.1-SNAPSHOT.jar org.jvi.weblogic.jms.App

结果

启动此命令后,我可以看到该消息在test队列中正确生成:

enter image description here