我可以使用哪些 Maven 依赖项为 Glassfish 创建独立的 JMS 客户端? [英] With which maven dependencies can i create a standalone JMS client for Glassfish?

查看:14
本文介绍了我可以使用哪些 Maven 依赖项为 Glassfish 创建独立的 JMS 客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

My project is built using maven.

我知道在使用 JMS 依赖项方面似乎有些混乱,因此,我应该在 pom 中使用哪些依赖项

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. 连接到我的 JNDI 上下文
  2. 能够阅读我的 JMS 主题吗?

我的 Java 测试方法是

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

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

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>

推荐答案

好的,这个方案相当棘手.

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

然后,我用

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

不是一点点变化,因为gf-client为Glassfish拉所有罐子,这显然制作了很多罐子(希望有 一种方法 优化 jar 数,虽然我们都知道过早优化).

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

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

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>

关联到 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上传工件页面"在我们公司的 3rd 方本地存储库中创建这些依赖项.

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

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

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>

而且我可以完全轮询我的 JMS 队列.

And I can totally poll my JMS queue.

这篇关于我可以使用哪些 Maven 依赖项为 Glassfish 创建独立的 JMS 客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆