検索

このコンテンツは選択した言語では利用できません。

Chapter 5. Examples

download PDF

This chapter demonstrates the use of AMQ JMS Pool through example programs.

For more examples, see the {ClientExamplesLink}.

5.1. Prerequisites

5.2. Establishing a connection

This example creates a new connection pool, binds it to a connection factory, and uses the pool to create a new connection.

Example: Establishing a connection - Connect.java

package net.example;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;

public class Connect {
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: Connect <connection-uri>");
            System.exit(1);
        }

        String connUri = args[0];

        ConnectionFactory factory = new JmsConnectionFactory(connUri);
        JmsPoolConnectionFactory pool = new JmsPoolConnectionFactory();

        try {
            pool.setConnectionFactory(factory);

            Connection conn = pool.createConnection();

            conn.start();

            try {
                System.out.println("CONNECT: Connected to '" + connUri + "'");
            } finally {
                conn.close();
            }
        } finally {
            pool.stop();
        }
    }
}

5.3. Configuring the pool

This example demonstrates setting connection and session configuration options.

Example: Configuring the pool - ConnectWithConfiguration.java

package net.example;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import org.apache.qpid.jms.JmsConnectionFactory;
import org.messaginghub.pooled.jms.JmsPoolConnectionFactory;

public class ConnectWithConfiguration {
    public static void main(String[] args) throws Exception {
        if (args.length != 1) {
            System.err.println("Usage: ConnectWithConfiguration <connection-uri>");
            System.exit(1);
        }

        String connUri = args[0];

        ConnectionFactory factory = new JmsConnectionFactory(connUri);
        JmsPoolConnectionFactory pool = new JmsPoolConnectionFactory();

        try {
            pool.setConnectionFactory(factory);

            // Set the max connections per user to a higher value
            pool.setMaxConnections(5);

            // Create a MessageProducer for each createProducer() call
            pool.setUseAnonymousProducers(false);

            Connection conn = pool.createConnection();

            conn.start();

            try {
                System.out.println("CONNECT: Connected to '" + connUri + "'");
            } finally {
                conn.close();
            }
        } finally {
            pool.stop();
        }
    }
}

5.4. Running the examples

To compile and run the example programs, use the following procedure.

Procedure

  1. Create a new project directory. This is referred to as <project-dir> in the steps that follow.
  2. Copy the example Java listings to the following locations:

    <project-dir>/src/main/java/net/example/Connect.java
    <project-dir>/src/main/java/net/example/ConnectWithConfiguration.java
  3. Use a text editor to create a new <project-dir>/pom.xml file. Add the following XML to it:

    <project>
      <modelVersion>4.0.0</modelVersion>
    
      <groupId>net.example</groupId>
      <artifactId>example</artifactId>
      <version>1.0.0-SNAPSHOT</version>
    
      <dependencies>
        <dependency>
          <groupId>org.messaginghub</groupId>
          <artifactId>pooled-jms</artifactId>
          <version>1.1.0.redhat-00002</version>
        </dependency>
        <dependency>
          <groupId>org.apache.qpid</groupId>
          <artifactId>qpid-jms-client</artifactId>
          <version>0.48.0.redhat-00004</version>
        </dependency>
      </dependencies>
    </project>
  4. Change to the project directory and use the mvn command to compile the program.

    mvn clean package dependency:copy-dependencies -DincludeScope=runtime -DskipTests

    The addition of dependency:copy-dependencies results in the dependencies being copied into the target/dependency directory.

  5. Use the java command to run the program.

    On Linux or UNIX:

    java -cp "target/classes:target/dependency/*" net.example.Connect amqp://localhost

    On Windows:

    java -cp "target\classes;target\dependency\*" net.example.Connect amqp://localhost

These sample commands run the Connect example. To run another example, replace Connect with the class name of your desired example.

Running the Connect example on Linux results in the following output:

$ java -cp "target/classes:target/dependency/*" net.example.Connect amqp://localhost
CONNECT: Connected to 'amqp://localhost'
Red Hat logoGithubRedditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

© 2024 Red Hat, Inc.