Mule ESB中entry-point-resolver的使用(8) Custom Entry Point Resolver

浪尽此生 提交于 2019-12-03 10:28:59

     前面介绍了6种Mule ESB提供的Entry Point Resolver,如果在实际应用中觉得这些Entry Point Resolver不能满足实际需要,可以自定义Entry Point Resolver。自定义的Entry Point Resolver类必须继承抽象类

org.mule.model.resolvers.AbstractEntryPointResolver

     我们这里实现一个自定义Entry Point Resolver,根据用户传入的两个操作数和操作符参数,分别执行不同的计算方法,返回操作结果。

    我们计算传入的参数是json格式   

{
   "operand1":13,
   "operand2":40,
   "operator":"add"   
}

    我们首先定义一个新的Flow,命名为Caculate_Flow

    

   入口的Http Listener节点仍使用8081端口,为了和前面的Flow区别,Path改为Calc

   

   流程中的Byte Array to String Transformer是把Http请求的InputStream类型的Payload转换为String类型的Payload。Json Translate Transformer使用了我们自定义的Transformer类CalcTransformer,把json报文转换为Object数组   

public class CalcTransformer extends AbstractMessageTransformer {

	private Logger logger = LogManager.getLogger(CalcTransformer.class);
	
	@Override
	public Object transformMessage(MuleMessage message, String outputEncoding) 
throws TransformerException {		
		try 
		{
			String messageStr = message.getPayloadAsString();
			JSONObject jsonObj = JSON.parseObject(messageStr);
			
			Object[] paramArray = new Object[3];
			String param1 = jsonObj.getString("operand1");
			String param2 = jsonObj.getString("operand2");
			String param3 = jsonObj.getString("operator");
			paramArray[0] = Integer.parseInt(param1);
			paramArray[1] = Integer.parseInt(param2);
			paramArray[2] = param3;		
			
			return paramArray;
			
		} catch (Exception e) {
			logger.error(ExceptionUtils.getFullStackTrace(e));
		}
		return null;
	}
}

Caculate Component使用我们自定义的CalcComponent类,用于计算,我们定义了加,减,乘,除(整数除)四个方法

public class CalcComponent 
{	
	public Long add(Integer number1, Integer number2)
	{
		return (long)(number1 + number2);
	}
	
	public Integer sub(Integer number1, Integer number2)
	{
		return (number1 - number2);
	}
	
	public Long mul(Integer number1, Integer number2)
	{
		return (long)(number1 * number2);
	}
	
	public Integer div(Integer number1, Integer number2)
	{
		return number1 / number2;
	}
}

最后定义的Logger输出计算结果

我们在Component节点中配置使用Custom Entry Point Resolver

<component doc:name="Caculate Component">        	
    <custom-entry-point-resolver class="entrypointresolvertest.CustomEntryPointResolver"/>
    <spring-object bean="customEntryTest"/>   
</component>

CustomEntryPointResolver的实现代码如下:

public class CustomEntryPointResolver extends AbstractEntryPointResolver {

	private Logger logger = LogManager.getLogger(CustomEntryPointResolver.class);
	
	@Override
	public InvocationResult invoke(Object component, MuleEventContext context) throws Exception {
		
		try
		{
			Object[] payload = getPayloadFromMessage(context);
			
			if(payload != null && payload.length == 3)
			{
					int number1 = Integer.parseInt(payload[0].toString());
					int number2 = Integer.parseInt(payload[1].toString());				
					String operandName = payload[2].toString();
					Class<?>[] classTypes = ClassUtils.getClassTypes(payload);
					Class<?>[] paramTypes = new Class<?>[2];
					for(int i=0; i<2;i++)
					{
						paramTypes[0] = classTypes[0];
						paramTypes[1] = classTypes[1];
					}
					Method method = getMethodByName(component, operandName, context);
					if (method == null)
			        {
			            method = ClassUtils.getMethod(component.getClass(), operandName, paramTypes, true);
			        }
			        if (method != null)
		            {
		                addMethodByName(component, method, context);	
		                
	                	Object[] arguments = new Object[2];
	                	arguments[0] = number1;
	                	arguments[1] = number2;
	                	InvocationResult result = invokeMethod(component, method, arguments);
	                	return result;
		            }
			        else
			        {
			        	InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
				        result.setErrorNoMatchingMethods(component, classTypes);
				        return result;
			        }
			}
			else
			{
				Class<?>[] classTypes = ClassUtils.getClassTypes(payload);
				InvocationResult result = new InvocationResult(this, InvocationResult.State.FAILED);
		        result.setErrorNoMatchingMethods(component, classTypes);
		        return result;
			}
		}
		catch(Exception ex)
		{
			logger.error(ExceptionUtils.getFullStackTrace(ex));
		}		
		return null;
	}

}

   具体的实现代码参考了Method Entry Point Resolver和Reflection Entry Point Resolver的源码,主要思想是根据传送的第三个操作符参数,找到CalcComponent对应的运算方法执行,获得结果。

启动项目,分别传送加,减,乘,除的报文,结果如下

加法

{
  "operand1":153,
  "operand2":251,
  "operator":"add"
}

执行结果

减法

{
  "operand1":540,
  "operand2":1500,
  "operator":"sub"
}

执行结果

乘法

{
  "operand1":44100,
  "operand2":14343,
  "operator":"mul"
}

执行结果

除法

{
  "operand1":1534337,
  "operand2":24503,
  "operator":"div"
}

执行结果

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!