`
yarafa
  • 浏览: 86888 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JBoss ESB学习笔记16——第十五个ESB应用Web Service Consumer 1

阅读更多

续上篇介绍了第十四个ESB应用,本文介绍第十五个ESB应用——Web Service Consumer 1。

 

说明:本文及后续文章虽非百分百的原创,但毕竟包含本人的努力和付出,所以希望大家转载时务请注明出处:http://yarafa.iteye.com/,谢谢合作。

 

 

1 概述
本实例主要演示了如何配置ESB的Action来调用基于JSR181风格注释的Web Service,即ESB的Action用作Web Service的客户端。

 

2 新建ESB工程
操作过程略。

 

3 ESB配置
3.1 创建消息队列

<?xml version="1.0" encoding="UTF-8"?>
<server>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=webserviceconsumer1Esb" xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
		</depends>
		<depends>jboss.messaging:service=PostOffice</depends>
	</mbean>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=webserviceconsumer1EsbReply" xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
		</depends>
		<depends>jboss.messaging:service=PostOffice</depends>
	</mbean>
	<mbean code="org.jboss.jms.server.destination.QueueService"		name="jboss.esb.quickstart.destination:service=Queue,name=webserviceconsumer1RequestGw" xmbean-dd="xmdesc/Queue-xmbean.xml">
		<depends optional-attribute-name="ServerPeer">
jboss.messaging:service=ServerPeer
		</depends>
		<depends>jboss.messaging:service=PostOffice</depends>
	</mbean>
</server>

 

3.2 定义Provider
这里将定义一个JMS Provider,并定义2个消息通道,内容如下:

<jms-provider connection-factory="ConnectionFactory" name="JBossMQ">
	<jms-bus busid="gwChanel">
<jms-message-filter dest-type="QUEUE"
	dest-name="queue/webserviceconsumer1RequestGw" />
	</jms-bus>
	<jms-bus busid="esbChanel">
		<jms-message-filter dest-name="queue/webserviceconsumer1Esb"
			dest-type="QUEUE" />
	</jms-bus>
</jms-provider>

 

3.3 定义Service

<service category=" Webservice" description=" Webservice"
name=" WebserviceConsumer1">
	<listeners />
	<actions mep="OneWay" />
</service>

 

3.4 定义Listener

<listeners>
	<jms-listener busidref="gwChanel" is-gateway="true"
		name="gwListener" />
	<jms-listener busidref="esbChanel" name="esbListener" />
</listeners>

 

3.5 定义Action类
3.5.1 MyRequestAction

/***********************************************************************
 * <p>Project Name: webserviceconsumer1</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.MyRequestAction.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.action;

import java.util.HashMap;

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Message;

/**
 * <p>Class Name: MyRequestAction</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-26
 * @version 1.0
 */
public class MyRequestAction extends AbstractActionLifecycle
{
	protected ConfigTree configTree;
	
	public MyRequestAction(ConfigTree configTree)
	{
		this.configTree = configTree;
	}
	
	public Message process(Message message) throws Exception
	{
		System.out.println("Receiving Messages From Client");
		String body = (String) message.getBody().get();
		HashMap<String, Object> request = new HashMap<String, Object>();
		
		// add parameters to the web service request map
		request.put("sayHello.toWhom", body);
		message.getBody().add(request);
		System.out.println("Request map is: " + request.toString());
		
		return message;
	}
	
	public void exceptionHandler(Message message, Throwable exception)
	{
		System.out.println("!ERROR!");
		System.out.println(exception.getMessage());
		System.out.println("For Message: ");
		System.out.println(message.getBody().get());
	}
}

 

说明:request.put("sayHello.toWhom", body);,该语句将客户端发来的消息内容作为要调用的Web Service中sayHello方法的参数,交由下一个action进行处理。

 

3.5.2 MyResponseAction

/***********************************************************************
 * <p>Project Name: webserviceconsumer1</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.action.MyResponseAction.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.action;

import java.util.Map;

import org.jboss.soa.esb.actions.AbstractActionLifecycle;
import org.jboss.soa.esb.helpers.ConfigTree;
import org.jboss.soa.esb.message.Body;
import org.jboss.soa.esb.message.Message;

/**
 * <p>Class Name: MyResponseAction</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-26
 * @version 1.0
 */
public class MyResponseAction extends AbstractActionLifecycle
{
protected ConfigTree configTree;
	
	public MyResponseAction(ConfigTree configTree)
	{
		this.configTree = configTree;
	}
	
	@SuppressWarnings("unchecked")
	public Message process(Message message) throws Exception
	{
		Map<String, Object> response = (Map<String, Object>) message.getBody().get(Body.DEFAULT_LOCATION);
		System.out.println("Response Map is: " + response);
		
		return message;
	}
	
	public void exceptionHandler(Message message, Throwable exception)
	{
		System.out.println("!ERROR!");
		System.out.println(exception.getMessage());
		System.out.println("For Message: ");
		System.out.println(message.getBody().get());
	}
}

 

说明:该action用于打印输出调用Web Service的返回结果。

 

3.6 发布Web Services
3.6.1 创建Dynamic Web Project

创建过程略。

 

3.6.2 创建服务类

/***********************************************************************
 * <p>Project Name: webservice</p>
 * <p>File Name: com.thu.afa.ws.HelloWorldWS.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.ws;

import java.util.Date;

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

/**
 * <p>Class Name: HelloWorldWS</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-26
 * @version 1.0
 */
@WebService
(
	name = "HelloWorld",
	targetNamespace = "http://afa.thu.com/ws/helloworld"
)
public class HelloWorldWS
{
	@WebMethod
	public String sayHello(@WebParam(name = "toWhom") String toWhom)
	{
		String greeting = "Hello World Greeting for '" + toWhom + "' on " + new Date();
		
		return greeting;
	}
}

 

注意:注释语句@WebParam(name = "toWhom")不可少,否则,在调用服务时将找不到参数。

 

3.6.3 配置服务
在web.xml文件中添加如下配置信息即可将服务类中指定的方法发布为Web Service。

<servlet>
	<servlet-name>helloWorldWS</servlet-name>
	<servlet-class>com.thu.afa.ws.HelloWorldWS</servlet-class>
</servlet>
<servlet-mapping>
	<servlet-name>helloWorldWS</servlet-name>
	<url-pattern>/servlet/helloWorldWS</url-pattern>
</servlet-mapping>

 

3.6.4 发布
将工程导出为war包并部署到web服务器的部署目录即可。如欲查看服务是否已发布,可在浏览器中浏览如下地址:http://localhost:8080/jbossws/services,这里是将工程发布到JBoss应用服务器或者JBoss ESB服务器后的查看地址。

 

3.7 配置Action

<action class="com.thu.afa.esb.jbossesb.action.MyRequestAction"
	name="request-mapper" process="process" />
<action class="org.jboss.soa.esb.actions.soap.SOAPClient"
	name="soapClientAction">
	<property name="wsdl"
value="http://localhost:8080/webservice/servlet/helloWorldWS?wsdl" />
	<property name="SOAPAction" value="sayHello" />
	<property name="responseAsOgnlMap" value="true" />
</action>
<action name="response-mapper" process="process"
class="com.thu.afa.esb.jbossesb.action.MyResponseAction" />

 

注意:在第二个action中,SOAPAction属性表明要调用的服务中的方法名称,不能用operation属性,否则,在部署esb时就会出错。

 

3.8 配置部署文件
部署依赖文件deployment.xml内容如下:

<jbossesb-deployment>
	<depends>jboss.esb:deployment=soap.esb</depends>
	<depends>jboss.esb:service=SoapUIClientService</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=webserviceconsumer1Esb
	</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=webserviceconsumer1EsbReply
	</depends>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=webserviceconsumer1RequestGw
	</depends>
</jbossesb-deployment>

 

3.9 部署ESB
将整个工程导出成一个ESB文件,并保存至JBoss ESB Server的部署目录下,启动JBoss ESB Server即可。

 

4 ESB客户端
4.1 新建Java工程

这里略去操作过程以及添加所需要的Jar包,具体操作过程可参考第一个ESB实例说明。

 

4.2 发送消息的客户端

/***********************************************************************
 * <p>Project Name: helloworldclient</p>
 * <p>File Name: com.thu.afa.esb.jbossesb.client.WebServiceConsumer1.java</p>
 * <p>Copyright: Copyright (c) 2010</p>
 * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>
 ***********************************************************************/
package com.thu.afa.esb.jbossesb.client;

import java.util.Properties;

import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * <p>Class Name: WebServiceConsumer1</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-26
 * @version 1.0
 */
public class WebServiceConsumer1Client
{
	private QueueConnection connection;
	private QueueSession session;
	private Queue queue;
	
	public void setupConnection() throws Exception
	{
		Properties properties = new Properties();
		properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");   
		properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");   
		properties.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
		InitialContext context = new InitialContext(properties);
		
		QueueConnectionFactory factory = (QueueConnectionFactory) context.lookup("ConnectionFactory");
		connection = factory.createQueueConnection();
		queue = (Queue) context.lookup("queue/webserviceproducerGw");
		//queue = (Queue) context.lookup("queue/webserviceconsumer1RequestGw");
		session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
		connection.start();
		
		System.out.println("Connection Started");
	}
	
	public void stop() throws Exception
	{
		if(connection != null) connection.stop();
		if(session != null) session.close();
		if(connection != null) connection.close();
		
		System.out.println("Client Stopped");
	}
	
	public void sendAMessage(String text) throws Exception
	{
		QueueSender sender = session.createSender(queue);
		ObjectMessage message = session.createObjectMessage(text);
		sender.send(message);
		sender.close();
	}

	/**
	 * <p>Title: </p>
	 * <p>Method Name: main</p>
	 * <p>Description: </p>
	 * @author: Afa
	 * @date: 2010-9-26
	 * @param args
	 */
	public static void main(String[] args) throws Exception
	{
		WebServiceConsumer1Client client = new WebServiceConsumer1Client();
		client.setupConnection();
		client.sendAMessage("Llu");
		client.stop();
	}

}

 

运行客户端程序即可看到如下输出结果:

JBoss ESB           

 

 

-----------------------------------------------------
Stay Hungry, Stay Foolish!
http://yarafa.iteye.com
Afa
Apr 15th, 2011
-----------------------------------------------------

 

分享到:
评论
10 楼 518ts520 2016-12-12  
03:07:24,852 INFO  [ServiceInvoker] Unresponsive EPR: InVMEpr [ PortReference < <wsa:Address invm://6b6e6f776c5365727669636524242424242424242424242466696e6453747564656e74537475647950726f63657373/false?false#10000/>, <wsa:ReferenceProperties jbossesb:passByValue : false/>, <wsa:ReferenceProperties jbossesb:type : urn:jboss/esb/epr/type/invm/> > ] for message: header: [  ]
03:07:54,854 INFO  [ServiceInvoker] Unresponsive EPR: InVMEpr [ PortReference < <wsa:Address invm://6b6e6f776c5365727669636524242424242424242424242466696e6453747564656e74537475647950726f63657373/false?false#10000/>, <wsa:ReferenceProperties jbossesb:passByValue : false/>, <wsa:ReferenceProperties jbossesb:type : urn:jboss/esb/epr/type/invm/> > ] for message: header: [  ]
03:07:54,854 INFO  [ServiceInvoker] Delivering message [header: [  ]] to DLQ.
03:07:54,860 ERROR [[findStudentStudyProcess]] Servlet.service() for servlet findStudentStudyProcess threw exception
org.jboss.soa.esb.listeners.message.ResponseTimeoutException: No response received for service [knowlService:findStudentStudyProcess].
at org.jboss.soa.esb.client.ServiceInvoker.post(ServiceInvoker.java:427)
at org.jboss.soa.esb.client.ServiceInvoker.deliverSync(ServiceInvoker.java:219)
at org.jboss.soa.esb.listeners.gateway.http.HttpGatewayServlet.processServiceRequest(HttpGatewayServlet.java:230)
at org.jboss.soa.esb.listeners.gateway.http.HttpGatewayServlet.service(HttpGatewayServlet.java:194)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:182)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:84)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:157)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:262)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:446)
at java.lang.Thread.run(Thread.java:745)

老大,
我的esb日志都是这些东西,看了网上一些资料,说是
jbossesb-server-4.10/server/default/deploy/jbossesb.sar/jbossesb-properties.xml的配置有问题。

另外还想问一下在
@WebService(name = "knowlService", serviceName = "knowlService", endpointInterface = "com.wondersgroup.teacher.service.KnowlService", targetNamespace = "http://entity.teacher.wondersgroup.com/")
注解的实现接口中,实体类都需要序列化吗?

我用的是jbossesb-server-4.10,监听是
<listeners>
<http-gateway name="addKnowl" urlPattern="/addKnowl/*" />
</listeners>
9 楼 usydapeng 2013-03-25  
谢谢您的分享,让我的学习进步的更快,希望你能在分享一点 JBoss ESB在项目开发中的使用经验
8 楼 yarafa 2012-10-27  
feng888918 写道
求一个 jboss esb学习笔记中 客户端 所需 jar  包  非常感谢
邮箱1024784402@qq.com

看我这个系列的第一篇文章吧,那里有说明
7 楼 feng888918 2012-10-13  
求一个 jboss esb学习笔记中 客户端 所需 jar  包  非常感谢
邮箱1024784402@qq.com
6 楼 bottor1234 2012-08-15  
楼主,你好
    现在我的程序执行时会告诉我一个警告:
[ActionProcessingPipeline] No fault address defined for fault message! To: JMSEpr [ PortReference < <wsa:Address jms:localhost:1099#queue/webserviceconsumer1Esb/>, <wsa:ReferenceProperties jbossesb:java.naming.factory.initial : org.jnp.interfaces.NamingContextFactory/>, <wsa:ReferenceProperties jbossesb:java.naming.provider.url : localhost:1099/>, <wsa:ReferenceProperties jbossesb:java.naming.factory.url.pkgs : org.jnp.interfaces/>, <wsa:ReferenceProperties jbossesb:destination-type : queue/>, <wsa:ReferenceProperties jbossesb:destination-name : queue/webserviceconsumer1Esb/>, <wsa:ReferenceProperties jbossesb:specification-version : 1.1/>, <wsa:ReferenceProperties jbossesb:connection-factory : ConnectionFactory/>, <wsa:ReferenceProperties jbossesb:persistent : true/>, <wsa:ReferenceProperties jbossesb:acknowledge-mode : AUTO_ACKNOWLEDGE/>, <wsa:ReferenceProperties jbossesb:transacted : false/>, <wsa:ReferenceProperties jbossesb:type : urn:jboss/esb/epr/type/jms/> > ] MessageID: 9fbdae0f-3870-4f88-be89-7ba607fe5c61 RelatesTo: jms:correlationID#9fbdae0f-3870-4f88-be89-7ba607fe5c61
WebService不能调用到,请问楼主有没有遇到过这样的问题
5 楼 lrh_java 2012-02-08  
博主有没有关于ESB整体部署的文档或者资料什么的,急求!
4 楼 yarafa 2011-05-01  
共同努力吧
3 楼 crabboy 2011-05-01  
期待尽快出研究成果
2 楼 yarafa 2011-04-17  
crabboy 写道
能否提供jbossESB、jbossRule和jbossJBPM的例子?

我和你一样,正在研究中
1 楼 crabboy 2011-04-17  
能否提供jbossESB、jbossRule和jbossJBPM的例子?

相关推荐

Global site tag (gtag.js) - Google Analytics