Apache CXF快速指南【JMS】

如前所述,您可以将CXF与JMS传输一起使用。在这种情况下,客户端会将JMS消息发送到已知的Messaging Server。我们的服务器应用程序一直在侦听消息传递服务器中的传入消息。消息到达时,它将处理该消息,执行客户端请求,并将响应作为另一条消息发送给客户端。

如前所述,我们将首先创建一个示例服务器应用程序,该应用程序提供一个称为sayHi的单一Web方法。

创建服务接口

我们的HelloWorld服务的服务界面如下所示-

//HelloWorld.java
package com.tutorialspoint.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.tutorialspoint.service.impl;

import javax.jws.WebService;
import com.tutorialspoint.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);

最后,我们在工厂中建立了Implementer类并开始运行它-

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

此时,服务器已启动并正在运行。注意,由于我们在POJO应用程序中使用了工厂bean类,因此不需要CXFServlet和web.xml文件。

完整的服务器应用程序代码如下所示-

//ServerJMS.java
package com.tutorialspoint.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.tutorialspoint.service.HelloWorld;
import com.tutorialspoint.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.tutorialspoint</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.tutorialspoint.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.tutorialspoint.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,该bean一直在监听此队列。

我们的下一个任务是创建一个客户端应用程序。

创建客户

在客户端应用程序中,首先我们设置与服务器应用程序中使用的JMS端点相同的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);

最后,我们调用service方法并打印其结果输出-

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

完整的客户端代码如下-

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

import com.tutorialspoint.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("TutorialsPoint");
      System.out.println(reply);
      System.exit(0);
   }
}

总结

CXF提供了一种统一的方法来混合n匹配几种当今用于创建Web应用程序的Web协议和传输。您学习了如何从传统的Java界面开始创建使用CXF的Web应用程序。接下来,您学习了如何从WSDL开始创建Web应用程序及其客户端。

WSDL提供服务接口的XML表示。您使用wsdl2java工具从WSDL创建Java接口,最后使用创建的接口编写了服务器和客户机。本教程还简要介绍了您在RESTful Web服务应用程序中使用CXF。最后,我们还讨论了如何将CXF与JMS一起使用。

作者:terry,如若转载,请注明出处:https://www.web176.com/apachecxf/215.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2020年10月22日 下午1:46
下一篇 2020年10月22日 下午2:04

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注