我可以使用哪些 Maven 依赖项为 Glassfish 创建独立的 JMS 客户端?

发布于 2024-11-01 17:20:49 字数 2545 浏览 1 评论 0原文

我想为我的 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>

i want to create a very simple JMS standalone client to a JMS Topic hosted on my Glassfish server.

My project is built using maven.

I know there seems to be some kind of mess concerning JMS dependencies to use, so, which dependencies shouls I use in my pom to

  1. Connect to my JNDI context
  2. Be able to read my JMS topic ?

My Java test method is

/** 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);
    }
}

And my pom contains, for now, the following dependencies

    <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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愛上了 2024-11-08 17:20:49

好吧,这个相当棘手。

经过一些搜索和尝试后,我删除了焊接依赖项(以便回到更经典的主干)。

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

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

This is not a Little Change,因为 gf-client 拉动了所有 Glassfish 的罐子,这显然会产生很多罐子(希望有 一种优化 jar 数量的方法,尽管我们都知道过早优化)。

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

<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 内部使用 imqbroker.jar,我建议您创建与 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 队列。

OK, this one was rather tricky.

After some searches and tries, I removed weld dependencies (in order to go back to a more classical main).

Then, I replaced my (old-style) appserv-rt.jar dependency with

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

This is not a little change, as gf-client pulls all jars for Glassfish, which obviously makes a lot of jars (hopefully there is a method to optimize jar number, although we all know about premature optimization).

So, once it's done, it's perfectly possible to use EJB remote interface, but not JMS (due to incomprehensible reasons). In order to make JMS work with gf-client, one then have to go create maven dependencies for imqjmsra.jar and imqbroker.jar, both located in %GLASSFISH3_INSTALL_DIR%/glassfish/lib/install/applications/jmsra. Furthermore, as imqjmsra.jar internally uses imqbroker.jar, I recommend you to create the following poms :

<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>

associated to imqjmsra.jar
and

<?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>

associated to imqbroker.jar.

Obviously, as I use Nexus repository management, it was easier for me to create these dependencies in our company 3rd parties local repository using Nexus "upload artifact page".

Once it's done, My POM dependencies now are

    <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>

And I can totally poll my JMS queue.

亣腦蒛氧 2024-11-08 17:20:49

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

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

您可以使用应用程序客户端容器自动生成的 Webstart 启动,也可以打包客户端并手动部署它。 Glassfish 3.0 手册、ACC

I'm converting from GF 2.1 to 3.1 and I haven't got to the client software yet (it is definitely on my list) but as far as I can tell you need most of the glassfish install with GF 3.1 to get the client to launch. (With GF 2.1 it was 15+ mb include files)

"gf-client.jar refers to many other .jars from the GlassFish installation directory so it is best to refer to it from within the installation directory itself rather than copying it(and all the other .jars) to another location" EJB FAQ

You can either use the auto-generated webstart launch with Application Client Container, or you can package up your client and deploy it manually. Glassfish 3.0 manual, ACC

一杯敬自由 2024-11-08 17:20:49

对我来说,有效的是从 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) 来解决这个问题:
使用 JMS 发送消息在退出时挂起

Well for me what worked was adding gf-client.jar from the glassfish installation folder, but adding gf-client from the maven repositories did not work.

When using maven dependencies, I found that this worked:
https://stackoverflow.com/a/10123034/516188

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

It makes for a 62Mb final JAR file, but it works. Next thing I hit this problem though, used System.exit(0) for that one:
Sending message with JMS hangs on exit

时光礼记 2024-11-08 17:20:49

您可以通过添加以下内容来获取 imqbroker:

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

You can get imqbroker by adding this:

    <dependency>
        <groupId>com.sun.messaging.mq</groupId>
        <artifactId>imqbroker</artifactId>
        <version>4.5.1-b03</version>
    </dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文