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

JBoss ESB学习笔记8——第七个ESB应用Https Gateway

阅读更多

续上篇介绍了第六个ESB应用,本文介绍第七个ESB应用——Https Gateway。

 

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

 

1概述
该实例主要演示了两点:一是配置一个https的ESB入口,二是在ESB外部端点中配置http路由器实现https传输方式的调用。

 

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


3 ESB配置
3.1 创建消息队列
这里将创建一个消息地理用于接收客户端消息,在esbcontent文件夹下创建文件jbm-queue-service.xml用于配置消息队列,内容如下:

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

 

3.2 定义Provider
这里将创建两个provider,一个提供客户端消息通道,另一个是jbr-provider,下面具体介绍。
3.2.1 jms-provider

<jms-provider connection-factory="ConnectionFactory" name="JMS">
	<jms-bus busid="httpsgatewayChanel">
		<jms-message-filter dest-name="queue/httpsgateway"
			dest-type="QUEUE" />
	</jms-bus>
</jms-provider>

 

3.2.2 jbr-provider

<jbr-provider host="localhost" name="httpsgateway" protocol="https">
	<property name="jbr-KeyStoreURL"
		value="C:/jbossesb-server-4.7/httpsgateway.keystore" />
	<property name="jbr-KeyStorePassword" value="123456" />
	<property name="jbr-TrustStoreURL"
		value="C:/jbossesb-server-4.7/httpsgateway.keystore" />
	<property name="jbr-TrustStorePassword" value="123456" />
	<property name="jbr-ClientAuthMode" value="need" />
	<property name="serviceInvokerTimeout" value="20000" />
	<jbr-bus busid="httpsgatewayJBRChanel" port="9433" />
</jbr-provider>

关于jbr-provider的配置如上所示,下面对各个属性做简要说明:

jbr-KeyStoreURL:密钥库文件的路径,下面会说明如何创建密钥库文件

jbr-KeyStorePassword:密钥库文件的密码,在创建密钥库文件时指定

jbr-TrustStoreURL:受信任的密钥库文件路径,可与jbr-KeyStoreURL属性值一样

jbr-TrustStorePassword:受信任的密钥库文件的密码

jbr-ClientAuthMode:客户端验证模式

serviceInvokerTimeout:服务调用时限

httpsgatewayJBRChanel:JBR端口设置

关于各个属性的详细说明,可参考官方文档或者相关API。

 

3.2.3 keystore制作

密钥库文件的制作如下图所示,这里使用的是JDK提供的keytool工具。关于如何使用keytool制作密钥库文件,请自行查找,网上到处都是,这里不再说明。

JBoss ESB

将制作好的keystore文件命名为httpsgateway.keystore,并保存至C:/jbossesb-server-4.7目录下。

 

3.2.4 配置属性文件
在esbcontent/ META-INF目录下新建属性文件HttpRouter.properties,其内容如下:

configurators=HttpProtocol
protocol-socket-factory=org.apache.commons.httpclient.contrib.ssl.EasySSLProtocolSocketFactory
keystore=C:/jbossesb-server-4.7/httpsgateway.keystore
keystore-passw=123456
truststore=C:/jbossesb-server-4.7/httpsgateway.keystore
truststore-passw=123456
max-connections-per-host=5

 

3.3定义Action类
3.3.1 Client Action

该类用于打印输出客户端响应的具体内容。

package com.thu.afa.esb.jbossesb.action.client;

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

import java.util.List;

public class HttpResponsePrinter extends AbstractActionLifecycle {

    protected ConfigTree configTree;

    public HttpResponsePrinter(ConfigTree config) {
        this.configTree = config;
    }


    public Message process(Message message) throws Exception {

        HttpResponse httpResponse = (HttpResponse) message.getBody().get(HttpResponse.RESPONSE_KEY);

        System.out.println("=========== Client Response: ===================================");
        System.out.println("Message Payload:");
        System.out.println("\t[" + message.getBody().get() + "]");

        System.out.println();
        System.out.println("Http Response:");
        System.out.println("\tCode: " + httpResponse.getResponseCode());
        System.out.println("\tLength: " + httpResponse.getLength());
        System.out.println("\tEncoding: " + httpResponse.getEncoding());

        System.out.println("\tHeaders:");
        List<HttpHeader> headers = httpResponse.getHttpHeaders();
        for (HttpHeader header : headers) {
            System.out.println("\t\t" + header.getName() + ": " + header.getValue());
        }

        System.out.println("================================================================");

        return message;
    }
}

 

3.3.2 Server Action

该类用于打印输出服务端请求的具体内容。

package com.thu.afa.esb.jbossesb.action.server;

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

public class HttpRequestPrinter extends AbstractActionLifecycle {

    protected ConfigTree configTree;

    public HttpRequestPrinter(ConfigTree config) {
        this.configTree = config;
    }


    public Message process(Message message) throws Exception {
        Properties properties = message.getProperties();

        System.out.println("=========== Server Request: ====================================");
        System.out.println("Message Payload:");
        System.out.println("\t[" + message.getBody().get() + "]");

        System.out.println();
        System.out.println("\tHeaders:");
        System.out.println("\t\thost: " + properties.getProperty("host"));
        System.out.println("\t\tMethod: " + properties.getProperty("MethodType"));
        System.out.println("\t\tPath: " + properties.getProperty("Path"));
        System.out.println("\t\tuser-agent: " + properties.getProperty("user-agent"));
        System.out.println("\t\tcontent-type: " + properties.getProperty("content-type"));
        System.out.println("================================================================");

        message.getBody().add("Http Response Payload!!");

        return message;
    }
}

 

3.4 定义第一个Service

<service category="HttpsService" description="Https Client"
	invmScope="GLOBAL" name="HttpsClientService">
	<listeners>
		<jms-listener busidref="httpsgatewayChanel" is-gateway="true"
			name="httpsgatewayListener" />
	</listeners>
	<actions mep="OneWay">
<action name="httprouter" class="org.jboss.soa.esb.actions.routing.http.HttpRouter">
			<property name="method" value="POST" />
			<property name="responseType" value="STRING" />
			<property name="endpointUrl"
value="https://localhost:9433/x/y">
				<http-client-property name="file"
value="/META-INF/HttpRouter-localhost-https-9433.properties" />
			</property>
			<property name="headers">
				<header name="name" value="llu" />
			</property>
		</action>
		<action name="printResponse"
class="com.thu.afa.esb.jbossesb.action.client.HttpResponsePrinter" />
	</actions>
</service>

配置说明:该服务执行了两次操作,首先将接收到的消息执行了一次http转发,然后由HttpResponsePrinter打印请求的内容。

 

3.5 定义第二个Service

<service category="HttpsService" description="Https Server"
	invmScope="GLOBAL" name="HttpsServerService">
	<listeners>
		<jbr-listener busidref="httpsgatewayJBRChanel"
			is-gateway="true" name="httpsgatewayJbsListener" />
	</listeners>
	<actions>
		<action name="printMessage" process="process"
class="com.thu.afa.esb.jbossesb.action.server.HttpRequestPrinter"/>
	</actions>
</service>

配置说明:该服务用语打印输出接收到的消息的内容。

 

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

<jbossesb-deployment>
	<depends>jboss.esb.quickstart.destination:service=Queue,name=httpsgateway
	</depends>
</jbossesb-deployment>

 

3.7 部署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.HttpsGatewayClient.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.Message;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

/**
 * <p>Class Name: HttpsGatewayClient</p>
 * <p>Description: </p>
 * @author Afa
 * @date 2010-9-7
 * @version 1.0
 */
public class HttpsGatewayClient
{
	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/httpsgateway");
		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();
	}
	
	public void sendAMessage(String text) throws Exception
	{
		QueueSender sender = session.createSender(queue);
		ObjectMessage message = session.createObjectMessage(text);
		sender.send(message);
		sender.close();
	}
	
	public void receiveMessage() throws Exception
	{
		QueueReceiver receiver = session.createReceiver(queue);
		Message message = receiver.receive();
		if(message != null)
		{
			if(message instanceof ObjectMessage)
			{
				ObjectMessage objectMessage = (ObjectMessage) message;
				System.out.println(objectMessage.getObject().toString());
			} else if (message instanceof TextMessage) {
				TextMessage textMessage = (TextMessage) message;
				System.out.println(textMessage.getText());
			}
		}
		receiver.close();
	}

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

 

运行客户端,结果如下图所示:

JBoss ESB

 

上述便是ESB第七个应用实例。如有问题,欢迎指正。

 

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

分享到:
评论
7 楼 usydapeng 2013-03-25  
https gateway部署成功,没有任何异常
但是,当运行客户端程序发送消息时,Eclipse console 中没有任何信息打印出来

请问这是什么问题??
6 楼 yarafa 2011-08-30  
看来你还没了解ESB的基本工作原理,一个service包含若干listener和一系列action,其中,listener是监听器,用于监听消息队列,远程目录等,一旦有消息到达监听器所监听的目的地,这该service中的action就会执行,而且是顺序执行的。

不同service中的action的执行是没有先后顺序的
5 楼 hesihua 2011-08-14  
这个例子有点不明白  密钥库文件是用来做什么用的呢   还有程序运行时 jboss-esb.xml文件的action执行顺序为什么不是顺序执行的 而是先执行第二个service中的action  后才执行第一个service中的方法的
4 楼 yarafa 2011-08-10  
问题解决了就好
3 楼 hesihua 2011-08-09  
嗯  现在解决了
第一个service的
<http-client-property name="file" 
14.value="/META-INF/HttpRouter-localhost-https-9433.properties" /> 

这行中的value值要修改为value="/META-INF/HttpRouter.properties"
2 楼 hesihua 2011-08-09  
希望笔主帮忙下
1 楼 hesihua 2011-08-09  
我做这个例子时服务器端的程序启动时有问题:
20:34:55,145 WARN  [ServiceController] Problem starting service jboss.esb:deploy
ment=sevenesb.esb
org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleException: Error configuri
ng action processing pipeline
        at org.jboss.soa.esb.listeners.message.MessageAwareListener.doInitialise
(MessageAwareListener.java:191)
        at org.jboss.soa.esb.listeners.lifecycle.AbstractManagedLifecycle.initia
lise(AbstractManagedLifecycle.java:133)
        at org.jboss.soa.esb.listeners.lifecycle.ManagedLifecycleController.init

相关推荐

    JBOSS_Esb学习资料

    7. **JMS路由**:《JBoss_ESB学习笔记9——第八个ESB应用JMS_Router.doc》和《JBoss_ESB学习笔记10——第九个ESB应用JMS_Topic.doc》涉及到了JMS(Java Message Service)在ESB中的应用,讲解了如何利用ESB进行消息...

    JBoss ESB 学习笔记

    #### 八、第七个ESB应用Https Gateway - **重点**:介绍如何通过Https Gateway组件实现安全的HTTPS通信。 - **实现**:设置SSL证书,确保数据传输的安全性。 #### 九、第八个ESB应用JMS Router - **重点**:学习...

    JBossESB学习笔记(1-16全)

    JBossESB学习笔记 收集了网上1-16系列教程,笔记详细介绍了JBossESB各个组件的特性及配置文件的说明

    JBossESB学习笔记.rar_Jboss_ESB_esb和aop

    【JBoss ESB 学习笔记】 JBoss ESB(Enterprise Service Bus,企业服务总线)是Red Hat公司开发的一款开源服务导向架构(SOA)平台,它为分布式应用程序提供了集成和互操作性。本笔记将深入探讨JBoss ESB的核心概念...

    Jboss_ESB学习笔记

    【JBoss ESB学习笔记】 JBoss ESB(Enterprise Service Bus)是Red Hat公司开发的一款开源企业服务总线,它是企业级应用集成的核心组件,用于连接不同系统、服务和应用程序,实现服务之间的通信和交互。本学习笔记...

    JBoss ESB学习笔记1-搭建ESB开发环境.docx

    JBoss ESB(Enterprise Service Bus,企业服务总线)是Red Hat公司提供的一个开源中间件,用于构建服务导向架构(SOA)。它提供了一个平台,让不同系统和服务之间能够通过标准接口进行通信,实现了服务之间的解耦合...

    JBoss ESB新手指南

    总之,《JBoss ESB新手指南》这本书将带领读者逐步了解和掌握这个强大的中间件平台,从基础概念到实战技巧,全面解析JBoss ESB在企业级集成中的应用。通过深入学习,新手可以快速成长为能够熟练运用JBoss ESB解决...

    Jboss-ESB学习笔记.doc

    本篇学习笔记主要围绕 JBoss ESB 的一个基础应用——“Hello World File Action”进行讲解,这个例子展示了如何利用 JBoss ESB 的 File Gateway 功能来监控文件系统变化,并通过 JMS(Java Message Service)消息...

    JbossESB4.6 程序开发

    在构建基于JBoss ESB 4.6的应用程序时,SOA(面向服务的架构)平台配置是至关重要的第一步。正确的配置能够确保服务之间高效稳定地交互。 - **环境搭建**:安装必要的软件包,如Java环境、JBoss ESB等,并进行版本...

    jboss esb 实例

    jboss esb 实例及讲解,我收集的资料整理做成书

    Jboss ESB简介及开发实例

    一、Jboss ESB的简介 1、 什么是ESB。 ESB的全称是Enterprise Service Bus,即企业服务总线。ESB是过去消息中间件的发展,ESB采用了“总线”这样一种模式来管理和简化应用之间的集成拓扑结构,以广为接受的开放...

    JbossESB开发环境配置.docx

    JBoss ESB 4.9需要一个特定的服务器环境,你可以从官方网站下载D:\jboss-5.1.0.GA/jbossesb-server,这是一个包含大量示例的独立运行版本。这些示例可以通过Ant脚本进行部署和测试: - 使用`ant deploy`命令来部署...

    JBOSS ESB程序员指南

    9. 第七章 Webservices支持 52 9.1. JBossWS 52 10. 第八章 Web Services编排 53 10.1. WS-BPEL 53 11. 第九章 服务日程安排 54 11.1. 介绍 54 11.2. 简单日程表 54 11.3. Cron日程表 54 11.4. 日程监听器 55 11.5. ...

    JBoss ESB 例子

    JBoss ESB 入门例子。主要是一个Server和一个Client。

    JBOSSESB学习小结

    NULL 博文链接:https://siye1982.iteye.com/blog/592400

    JBoss ESB 开发指南

    JBoss ESB(Enterprise Service Bus)是JBoss中间件项目的一个重要组成部分,主要功能在于提供一个灵活、可扩展的平台,用于构建和部署企业级集成解决方案。它支持多种消息传输协议和数据格式,能够处理复杂的消息流...

Global site tag (gtag.js) - Google Analytics