Apache-cxf-with-jms

提供:Dev Guides
移動先:案内検索

JMSを使用したApache CXF

前述のように、JMSトランスポートでCXFを使用できます。 この場合、クライアントは既知のMessaging ServerにJMSメッセージを送信します。 私たちのサーバーアプリケーションは、受信メッセージを求めてメッセージングサーバーを継続的にリッスンしています。 メッセージが到着すると、メッセージを処理し、クライアント要求を実行し、応答を別のメッセージとしてクライアントに送信します。

前述のように、 sayHi という単一のWebメソッドを提供するサンプルサーバーアプリケーションを最初に作成します。

サービスインターフェイスの作成

*HelloWorld* サービスのサービスインターフェイスを次に示します-
//HelloWorld.java
package com.finddevguides.service;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
   @WebMethod
   String sayHi(@WebParam(name = "name") String name);
}

サービスの実装

サービスインターフェイスの実装は次のように定義されています-

//HelloWorldImpl.java
package com.finddevguides.service.impl;

import javax.jws.WebService;
import com.finddevguides.service.HelloWorld;

@WebService
public class HelloWorldImpl implements HelloWorld {
   @Override
   public String sayHi(String name) {
      return "Hello " + name;
   }
}

実装は単純にHelloメッセージをユーザーに返します。 ご覧のとおり、インターフェイスとその実装は、これまで学習したこのチュートリアルの以前のすべてのプロジェクトに似ています。

次に、メッセージキューを設定し、着信メッセージをリッスンし続けるサーバーアプリケーションを作成することが最も重要なポイントになります。

サーバーの作成

サーバーアプリケーションでは、最初に次のように JMS エンドポイントを作成します-

private static final String JMS_ENDPOINT_URI =
   "jms:queue:test.cxf.jmstransport.queue?timeToLive=1000"
      + "&jndiConnectionFactoryName=ConnectionFactory"
      + "&jndiInitialContextFactory"
      + "= org.apache.activemq.jndi.ActiveMQInitialContextFactory"
      + "&jndiURL = tcp://localhost:61616";

指定された期間存続する指定されたポートにキューを設定することに注意してください。 org.apache.activemq.broker.BrokerService クラスをインスタンス化して、メッセージングサービスを作成します。 これは、 ActiveMQ メッセージングサーバーのサーバークラスです。

BrokerService broker = new BrokerService();
*ActiveMQ* 以外の任意のメッセージングサーバーを使用できます。 このサーバーを目的のURIに接続します。
broker.addConnector("tcp://localhost:61616");

私たちは、着信メッセージのデータストレージ用のディレクトリを設定します-

broker.setDataDirectory("target/activemq-data");

最後に、startメソッドを使用してサーバーを起動します-

broker.start();

次に、以前のPOJOアプリケーションで使用されているサーバーファクトリBeanクラスを使用して、サービスBean HelloWorld のインスタンスを作成します-

Object implementor = new HelloWorldImpl();
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWorld.class);

次に、ファクトリが受信メッセージをリッスンし続けるように、ファクトリにJMSエンドポイントを設定します-

factory.setTransportId
(JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
factory.setAddress(JMS_ENDPOINT_URI);

最後に、ファクトリで実装クラスを設定し、実行を開始します-

factory.setServiceBean(implementor);
factory.create();

この時点で、サーバーは稼働しています。 POJOアプリケーションのようにファクトリBeanクラスを使用しているため、CXFServletとweb.xmlファイルは不要であることに注意してください。

完全なサーバーアプリケーションコードはここに示されています-

//ServerJMS.java
package com.finddevguides.server;

import java.util.Collections;
import org.apache.cxf.ext.logging.LoggingFeature;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import org.apache.cxf.transport.jms.spec.JMSSpecConstants;
import com.finddevguides.service.HelloWorld;
import com.finddevguides.service.impl.HelloWorldImpl;
import org.apache.activemq.broker.BrokerService;

public final class ServerJMS {

   private static final String JMS_ENDPOINT_URI =
      "jms:queue:test.cxf.jmstransport.queue?timeToLive=1000"
         + "&jndiConnectionFactoryName=ConnectionFactory"
         + "&jndiInitialContextFactory"
         + "= org.apache.activemq.jndi.ActiveMQInitialContextFactory"
         + "&jndiURL = tcp://localhost:61616";

   public static void main(String[] args) throws Exception {

      BrokerService broker = new BrokerService();
      broker.addConnector("tcp://localhost:61616");
      broker.setDataDirectory("target/activemq-data");
      broker.start();

      Object implementor = new HelloWorldImpl();
      JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
      factory.setServiceClass(HelloWorld.class);
      factory.setTransportId
      (JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
      factory.setAddress(JMS_ENDPOINT_URI);
      factory.setServiceBean(implementor);
      factory.setFeatures(Collections.singletonList(new LoggingFeature()));
      factory.create();

      System.out.println("Server ready...");
      Thread.sleep(5 *60* 1000);
      System.out.println("Server exiting");
      System.exit(0);
   }
}

依存関係の追加

作成したサーバーアプリケーションは、ActiveMQメッセージングサーバーを使用します。 したがって、プロジェクトにいくつかの依存関係を追加する必要があります。 追加の必要な依存関係を理解するために、完全なpom.xmlファイルがここに表示されます。

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <modelVersion>4.0.0</modelVersion>
   <groupId>com.finddevguides</groupId>
   <artifactId>cxf-jms</artifactId>
   <version>1.0</version>
   <packaging>jar</packaging>

   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
   </properties>

   <profiles>
      <profile>
         <id>server</id>
         <build>
            <defaultGoal>test</defaultGoal>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <version>1.6.0</version>
                  <executions>
                     <execution>
                        <phase>test</phase>
                        <goals>
                           <goal>java</goal>
                        </goals>
                        <configuration>
                           <mainClass>
                              com.finddevguides.server.ServerJMS
                           </mainClass>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
            </plugins>
         </build>
      </profile>
      <profile>
         <id>client</id>
         <build>
            <defaultGoal>test</defaultGoal>
            <plugins>
               <plugin>
                  <groupId>org.codehaus.mojo</groupId>
                  <artifactId>exec-maven-plugin</artifactId>
                  <executions>
                     <execution>
                        <phase>test</phase>
                        <goals>
                           <goal>java</goal>
                        </goals>
                        <configuration>
                           <mainClass>
                              com.finddevguides.client.ClientJMS
                           </mainClass>
                        </configuration>
                     </execution>
                  </executions>
               </plugin>
            </plugins>
         </build>
      </profile>
   </profiles>

   <dependencies>
      <dependency>
         <groupId>org.apache.activemq</groupId>
         <artifactId>activemq-broker</artifactId>
         <version>5.15.8</version>
      </dependency>

      <dependency>
         <groupId>org.apache.activemq</groupId>
         <artifactId>activemq-kahadb-store</artifactId>
         <version>5.15.8</version>
      </dependency>

      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-frontend-jaxws</artifactId>
         <version>3.3.0</version>
      </dependency>

      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-transports-jms</artifactId>
         <version>3.3.0</version>
      </dependency>

      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-features-logging</artifactId>
         <version>3.3.0</version>
      </dependency>

      <dependency>
         <groupId>org.apache.cxf</groupId>
         <artifactId>cxf-rt-transports-http-jetty</artifactId>
         <version>3.3.0</version>
      </dependency>
   </dependencies>
</project>

実行中のサーバー

以前の場合のように、サーバーの実行を開始するには、コマンドウィンドウに次のコマンドを入力します-

mvn -Pserver

これにより、ActiveMQメッセージサーバーが起動し、メッセージングキューが設定され、このキューをリッスンし続けるファクトリBeanが作成されます。

次のタスクは、クライアントアプリケーションを作成することです。

クライアントを作成する

クライアントアプリケーションでは、最初にサーバーアプリケーションで使用されるものと同じJMSエンドポイントを設定します-

private static final String JMS_ENDPOINT_URI =
   "jms:queue:test.cxf.jmstransport.queue?timeToLive=1000"
      + "&jndiConnectionFactoryName=ConnectionFactory"
      + "&jndiInitialContextFactory"
      + " = org.apache.activemq.jndi.ActiveMQInitialContextFactory"
      + "&jndiURL = tcp://localhost:61616";

POJOアプリケーションのようにファクトリーを作成します。

JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

次のようにエンドポイントURIと実装クラスを設定します-

factory.setTransportId (JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
factory.setAddress (JMS_ENDPOINT_URI);
HelloWorld client = factory.create(HelloWorld.class);

最後に、サービスメソッドを呼び出して、その結果の出力を印刷します-

String reply = client.sayHi("finddevguides");
System.out.println(reply);

完全なクライアントコードは以下のとおりです-

//ClientJMS.java
package com.finddevguides.client;

import com.finddevguides.service.HelloWorld;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.transport.jms.spec.JMSSpecConstants;

public final class ClientJMS {
   private static final String JMS_ENDPOINT_URI =
   "jms:queue:test.cxf.jmstransport.queue?timeToLive=1000"
   + "&jndiConnectionFactoryName=ConnectionFactory"
   + "&jndiInitialContextFactory"
   + " = org.apache.activemq.jndi.ActiveMQInitialContextFactory"
   + "&jndiURL = tcp://localhost:61616";

   public static void main(String[] args) throws Exception {
      JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
      factory.setTransportId(JMSSpecConstants.SOAP_JMS_SPECIFICATION_TRANSPORTID);
      factory.setAddress(JMS_ENDPOINT_URI);
      HelloWorld client = factory.create(HelloWorld.class);
      String reply = client.sayHi("finddevguides");
      System.out.println(reply);
      System.exit(0);
   }
}