【问题标题】:Bare minimum code for a JMS producerJMS 生产者的最低限度代码
【发布时间】:2017-11-14 18:14:43
【问题描述】:

我真的很难为 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 放在类路径中,但我仍然无法克服这个 no class def found 异常。

有人可以链接到某个存储库或博客,或者为我提供一个最低限度的“工作”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)

【问题讨论】:

  • 所有东西都应该捆绑在 wlfullclient.jar 中。 weblogic的版本是多少?
  • 我正在使用 WL 12c
  • 好的。使用 wlthint3client 代替 wlfullclient。还有你为什么用 tcp 而不是 t3 协议呢?
  • @Rouliboy 我试过了,但现在我得到了Exception in thread "main" java.lang.NoClassDefFoundError: weblogic/security/service/PrivilegedActions,我会检查@Axel 的答案是否有这个异常
  • 您尝试过使用 t3 协议吗?你试过wlthint3client吗?

标签: java jms weblogic


【解决方案1】:

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

检查这里,这可能有答案:Weblogic 12.1.3 PrivilegedActions class not found

【讨论】:

  • 感谢您的回复。我正在尝试生成这个 JAR,但我遇到了一些错误.. 是否有一些存储库可以从那里下载 wlfullclient.jar JAR?
  • 等一下,我会得到的。
  • 我没有看到这个问题,我使用这里提到的过程生成了 WL 完整 JAR,但我仍然收到错误 docs.oracle.com/cd/E11035_01/wls100/client/jarbuilder.html
  • 您是否有任何独立的 JMS 生产者/消费者 Java 代码,包括所有 JAR?
【解决方案2】:

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

所以我将公开我所做的,只使用我自己的队列名称和工厂名称。

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

Weblogic 客户端

首先,我使用mvn install:install-file 命令从wlthint3client.jar 中的${ORACLE_HOME}/wlserver/server/lib 中的weblogic 服务器上创建了一个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 工厂和队列

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

JMS 生产者项目

然后我创建了 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 队列中正确生成:

【讨论】:

  • 非常感谢您发布这个有用的答案,请给我一些时间,我会检查并回复您。
  • 更多问题哥们,我不知道 Maven,我不知何故必须运行 Maven 命令,安装 maven,然后从我的 pom.xml 的位置(内容来自你的答案),我执行了您提到的命令,但运行失败,出现此错误 - [ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install-file (default-cli) on project jms-producer: The parameters 'file' for goal org.apache.maven.plugins:maven-install-plugin:2.4:install-file are missing or invalid -&gt; [Help 1]
  • 我运行了这个C:\ApacheFoundation\localRepository&gt;mvn install:install-file 的命令行,在“C:\ApacheFoundation\localRepository”我的 pom.xml 中包含了你提到的内容。为了成功运行 maven,我是否遗漏了什么或需要做些什么?
  • 您是从 WL 安装还是 Java 代码位置运行,我刚刚运行了更新的命令,但仍然收到相同的错误 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-install-plugin:2.4:install-file (default-cli) on project jms-producer: The specified file 'C:\Ap acheFoundation\localRepository\wlthint3client.jar' not exists -&gt; [Help 1]
  • 您从wlthint3client.jar jar 所在的位置运行此命令
猜你喜欢
  • 2021-12-31
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 2016-04-23
  • 2017-07-13
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多