Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

jakarta ee - With which maven dependencies can i create a standalone JMS client for Glassfish?

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...