[SOA] Mule ESB 3.x 入门(二)—— 配置(spring, properties, log4j)

Mule 中很好的结合了spring,在 Mule 3.x 中将 spring 3.x 作为核心组件,可以开箱即用,和一般J2EE应用开发无异。下面介绍一下:

一.  属性占位(property-placeholder)

Mule 利用 spring context:property-placeholder 就能实现属性替换。比如:下面的 ${address} 通过 test.properties 里的 address 属性替换。
将来开发有数据库操作的时候,就可以把数据库相关配置放在jdbc.properties里了。

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" 
      xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" 
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:spring="http://www.springframework.org/schema/beans" 
      version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
    
    <context:property-placeholder location="classpath:test.properties"></context:property-placeholder>
    
    <flow name="mule-maven-testFlow1" doc:name="mule-maven-testFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" address="http://${address}" doc:name="HTTP"/>
        <logger message="#[groovy:message.toString()]" level="INFO" doc:name="Logger Message"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy" file="scripts/build-response.groovy"/>
        </scripting:component>
    </flow>
</mule>

注意:新创建的 xxx.mflow 中并没有 xmlns:context 要用 context:property-placeholder 必须加上:

 

xmlns:context="http://www.springframework.org/schema/context"

 

因为 Mule 工程遵从 maven 规范,所以 test.properties 直接放在 src/main/resources 下即可。(还可以放在 src/main/app 下)

二. 读取 properties 
类似的,将某个properties放在 src/main/resources 下,就可以通过 spring 配置来读取它。因此我们实现一个 PropertyUtils 静态类来实现读取。

package com.neusoft.fx;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class PropertyUtils {
	
	private static Properties properties;
	
	public void init() {
		ClassLoader loader = Thread.currentThread().getContextClassLoader();
		InputStream resourceAsStream = loader.getResourceAsStream("message.properties");
		properties = new Properties();
		try {
			properties.load(resourceAsStream);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static String get(String key) {
		return (String)properties.get(key);
	}
}

mule 的配置:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" 
      xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" 
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" 
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:spring="http://www.springframework.org/schema/beans" 
      version="CE-3.3.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd 
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd ">
    <context:property-placeholder location="classpath:test.properties"></context:property-placeholder>
    <spring:beans>
    	<spring:bean id="propertyUtils" init-method="init" scope="singleton" class="com.neusoft.fx.PropertyUtils" name="Bean"/>
    </spring:beans>
    <flow name="mule-maven-testFlow1" doc:name="mule-maven-testFlow1">
        <http:inbound-endpoint exchange-pattern="request-response" address="http://${address}" doc:name="HTTP"/>
        <logger message="#[groovy:message.toString()]" level="INFO" doc:name="Logger Message"/>
        <scripting:component doc:name="Groovy">
            <scripting:script engine="Groovy" file="scripts/build-response.groovy"/>
        </scripting:component>
        <logger message="#[groovy:message.toString()]" level="INFO" doc:name="Logger Message"/>
    </flow>
</mule>

在 groovy 中也可以直接使用:

import com.neusoft.fx.*;

def contentType = message.getInboundProperty("Content-Type");
def response = "";
if (contentType == "application/xml") {
	response = PropertyUtils.get("response.xml");
} else {
	response = PropertyUtils.get("response.json");
}

response


三. log4j

 

通过 Mule IDE 创建的 mule 工程中并没有log4j的配置文件,直接拷贝一份到 src/main/resources 下即可实现对 mule 应用的log配置。
例:

 

# Default log level
log4j.rootCategory=INFO,console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-5p %d [%t] %c: %m%n

#log4j.appender.INFO=org.apache.log4j.ConsoleAppender
#log4j.appender.INFO.layout=org.apache.log4j.PatternLayout
#log4j.appender.INFO.layout.ConversionPattern=%-5p %d [%t] %c: %m%n

################################################
# You can set custom log levels per-package here
################################################

# CXF is used heavily by Mule for web services
log4j.logger.org.apache.cxf=WARN

# Apache Commons tend to make a lot of noise which can clutter the log.
log4j.logger.org.apache=WARN

# Reduce startup noise
log4j.logger.org.springframework.beans.factory=WARN

# Mule classes
log4j.logger.org.mule=INFO

# Your custom classes
log4j.logger.com.taiping.esb=INFO

 

 

 


转载于:https://www.cnblogs.com/bbsno1/p/3253600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值