【问题标题】:With which maven dependencies can i create a standalone JMS client for Glassfish?我可以使用哪些 maven 依赖项为 Glassfish 创建独立的 JMS 客户端?
【发布时间】:2011-08-06 05:03:50
【问题描述】:

我想为托管在我的 Glassfish 服务器上的 JMS 主题创建一个非常简单的 JMS 独立客户端。

我的项目是使用 maven 构建的。

我知道关于要使用的 JMS 依赖项似乎有些混乱,所以,我应该在我的 pom 中使用哪些依赖项

  1. 连接到我的 JNDI 上下文
  2. 能够阅读我的 JMS 主题吗?

我的 Java 测试方法是

/** Thanks to WELD CDI, this method is not static */
public void main(@Observes ContainerInitialized event) throws Throwable {
    Context context = new InitialContext();
    ConnectionFactory factory = (ConnectionFactory) context.lookup(JMSNotifierConstants.CONNECTION_FACTORY_NAME);
    Connection connection = factory.createConnection();
    Topic topic = (Topic) context.lookup(JMSNotifierConstants.NOTIFICATION_TOPIC);
    Session session = connection.createSession(false,
            Session.AUTO_ACKNOWLEDGE);
    MessageConsumer consumer = session.createConsumer(topic);
    connection.start();
    while (true) {
        Message received = consumer.receive();
        System.out.println(received);
    }
}

我的 pom 目前包含以下依赖项

    <dependency>
        <groupId>javax.enterprise</groupId>
        <artifactId>cdi-api</artifactId>
        <version>1.0-SP1</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld</groupId>
        <artifactId>weld-se</artifactId>
        <version>1.0.1-Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.weld</groupId>
        <artifactId>weld-logger</artifactId>
        <version>1.0.0-CR2</version>
        <type>jar</type>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-jdk14</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.jms</artifactId>
        <version>3.0</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.glassfish.extras</groupId>
        <artifactId>appserv-rt</artifactId>
        <version>3.1</version>
    </dependency>

【问题讨论】:

    标签: jakarta-ee maven glassfish jms


    【解决方案1】:

    对我来说有效的是从 glassfish 安装文件夹中添加 gf-client.jar,但从 maven 存储库中添加 gf-client 不起作用。

    当使用 maven 依赖项时,我发现这行得通: https://stackoverflow.com/a/10123034/516188

    <dependency>
        <groupId>org.glassfish.main.extras</groupId>
        <artifactId>glassfish-embedded-all</artifactId>
        <version>3.1.2</version>
    </dependency>
    

    它会生成一个 62Mb 的最终 JAR 文件,但它可以工作。接下来我遇到了这个问题,使用 System.exit(0) 来解决这个问题: Sending message with JMS hangs on exit

    【讨论】:

      【解决方案2】:

      你可以通过添加这个来获得imqbroker:

          <dependency>
              <groupId>com.sun.messaging.mq</groupId>
              <artifactId>imqbroker</artifactId>
              <version>4.5.1-b03</version>
          </dependency>
      

      【讨论】:

        【解决方案3】:

        好的,这个有点棘手。

        经过一些搜索和尝试,我删除了焊接依赖项(为了回到更经典的 main)。

        然后,我将我的(旧式)appserv-rt.jar 依赖替换为

            <dependency>
                <groupId>org.glassfish.appclient</groupId>
                <artifactId>gf-client</artifactId>
                <version>3.1</version>
                <type>pom</type>
                <scope>compile</scope>
            </dependency>
        

        不是一点变化,因为gf-client为Glassfish拉出所有 jars,这显然会产生很多jar(希望有a method可以优化jar 号,虽然我们都知道过早优化)。

        所以,一旦完成,完全可以使用 EJB 远程接口,但 不是 JMS(由于无法理解的原因)。为了使 JMS 与 gf-client 一起工作,必须为 imqjmsra.jarimqbroker.jar 创建 maven 依赖项,它们都位于 %GLASSFISH3_INSTALL_DIR%/glassfish/lib/install/applications/jmsra。此外,由于imqjmsra.jar 内部使用imqbroker.jar,我建议您创建以下pom:

        <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <modelVersion>4.0.0</modelVersion>
          <groupId>org.glassfish.external.jms</groupId>
          <artifactId>imqjmsra</artifactId>
          <version>3.1.0</version>
          <description>POM was created by Sonatype Nexus</description>
          <dependencies>
            <dependency>
                  <groupId>org.glassfish.external.jms</groupId>
                  <artifactId>imqbroker</artifactId>
                  <version>3.1.0</version>
            </dependency>
          </dependencies>
        </project>
        

        关联到imqjmsra.jar

        <?xml version="1.0" encoding="UTF-8"?>
        <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <modelVersion>4.0.0</modelVersion>
          <groupId>org.glassfish.external.jms</groupId>
          <artifactId>imqbroker</artifactId>
          <version>3.1.0</version>
          <description>POM was created by Sonatype Nexus</description>
        </project>
        

        关联到imqbroker.jar

        显然,当我使用 Nexus 存储库管理时,我更容易使用 Nexus“上传工件页面”在我们公司的第 3 方本地存储库中创建这些依赖项。

        一旦完成,我的 POM 依赖项现在是

            <dependency>
                <groupId>org.glassfish.appclient</groupId>
                <artifactId>gf-client</artifactId>
                <version>3.1</version>
                <type>pom</type>
                <scope>compile</scope>
            </dependency>
            <dependency>
                <groupId>org.glassfish.external.jms</groupId>
                <artifactId>imqjmsra</artifactId>
                <version>3.1.0</version>
            </dependency>
        

        我可以完全轮询我的 JMS 队列。

        【讨论】:

          【解决方案4】:

          我正在从 GF 2.1 转换到 3.1,但我还没有使用客户端软件(它肯定在我的列表中),但据我所知,您需要使用 GF 3.1 安装大部分 glassfish 才能获得客户端启动。 (使用 GF 2.1 包含 15+ mb 的文件)

          “gf-client.jar 引用了 GlassFish 安装目录中的许多其他 .jar,因此最好从安装目录本身引用它,而不是将其(以及所有其他 .jar)复制到另一个位置” EJB FAQ

          您可以将自动生成的 webstart 启动与应用程序客户端容器一起使用,也可以打包您的客户端并手动部署它。 Glassfish 3.0 manual, ACC

          【讨论】:

            猜你喜欢
            • 2015-10-27
            • 1970-01-01
            • 2015-01-02
            • 1970-01-01
            • 1970-01-01
            • 2011-09-11
            • 2013-02-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多