Mule ESB 学习笔记(20)Mule和Spring httpinvoker的整合

mule的配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:spring="http://www.springframework.org/schema/beans"
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
    xsi:schemaLocation="
       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
              http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">

    <spring:bean id="doSpringWork"
        class="com.easyway.mule.spring.httpinvoker.remoting.DoSomeWork" />

    <!--
        Transform a byte[] from an Http request into a Spring RemoteInvocation instance.
    -->
    <custom-transformer name="springInputXform"
        class="org.mule.module.spring.remoting.ObjectToRemoteInvocationTransformer" />
    <!--
        Convert an Object to a Spring RemoteInvocationResult and then into a byte[]
        for return to the caller via Http.
    -->
    <custom-transformer name="springOutputXform"
        class="org.mule.module.spring.remoting.ObjectToRemoteInvocationResultTransformer" />

    <model name="main">

        <!--
            SpringHttpInvokerComponent will receive a RemoteInvocation and execute
            the requested method on a POJO specified by "serviceClass".
        -->
        <service name="doSomeWorkViaSpring">

            <!--
                Listen on 8003 for Http requests.
                Use springInputXform to convert a byte[] from Http into a RemoteInvocation.
                Use springOutputXform to convert a Object from the Mule pipeline into a
                RemoteInvocationResult and then into a byte[].
            -->
            <inbound>
                <inbound-endpoint
                    address="http://localhost:8003/springService"
                    exchange-pattern="request-response">
                    <transformer ref="springInputXform" />
                    <response>
                        <transformer ref="springOutputXform" />
                    </response>
                </inbound-endpoint>
            </inbound>

            <pooled-component>
                <prototype-object
                    class="org.mule.module.spring.remoting.SpringRemoteInvokerComponent">
                    <properties>

                        <!--
                            Instead of setting the class, provide SpringHttpInvokerComponent
                            with a Spring managed bean instead.
                            
                            (This doesn't work yet because I don't know how to teach
                            SpringHttpInvokerComponent to find Spring beans.)
                        -->
                        <spring:entry key="serviceBean"
                            value-ref="doSpringWork" />

                        <!--
                            Set the serviceInterface property of Spring's RemoteInvocationBasedExporter
                        -->
                        <spring:entry key="serviceInterface"
                            value="com.easyway.mule.spring.httpinvoker.remoting.WorkInterface" />

                        <!--
                            Set other properties of Spring's RemoteInvocationBasedExporter
                            i.e. registerTraceInterceptor and remoteInvocationExecutor
                        -->
                    </properties>
                </prototype-object>
            </pooled-component>

        </service>
    </model>
</mule>

 

 

package com.easyway.mule.spring.httpinvoker.remoting;

import org.mule.util.StringUtils;

import java.io.Serializable;

public class ComplexData implements Serializable
{
    private static final long serialVersionUID = -886414019167115007L;

    private String someString = "Default String";
    private Integer someInteger = new Integer(13);

    public ComplexData()
    {
        super();
    }

    public ComplexData(String someString, Integer someInteger)
    {
        super();
        setSomeString(someString);
        setSomeInteger(someInteger);
    }

    public String toString()
    {
        try
        {
            String currentString = StringUtils.defaultIfEmpty(someString, "NULL");
            return "[ComplexData: [someString=" + currentString + "][someInteger=" + someInteger + "]]";
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
    }

    public Integer getSomeInteger()
    {
        return someInteger;
    }

    public void setSomeInteger(Integer someInteger)
    {
        this.someInteger = someInteger;
    }

    public String getSomeString()
    {
        return someString;
    }

    public void setSomeString(String someString)
    {
        this.someString = someString;
    }

}

 

 

package com.easyway.mule.spring.httpinvoker.remoting;

/**
 * A server-side service to receive and process ComplexData.
 */
public class DoSomeWork implements WorkInterface
{
    public String executeByteArray(byte[] input)
    {
        return executeString(new String(input));
    }

    public String executeString(String input)
    {
        return "You said " + input;
    }

    public ComplexData executeComplexity(ComplexData input)
    {
        input.setSomeString(input.getSomeString() + " Received");
        input.setSomeInteger(new Integer(input.getSomeInteger().intValue() + 1));
        return input;
    }
}

 

 

package com.easyway.mule.spring.httpinvoker.remoting;

public interface WorkInterface
{
    String executeByteArray(byte[] input);

    String executeString(String input);

    ComplexData executeComplexity(ComplexData input);
}

 

 

服务端测试:

package com.easyway.httpinvoker;


import org.mule.api.MuleContext;
import org.mule.api.context.MuleContextFactory;
import org.mule.config.spring.SpringXmlConfigurationBuilder;
import org.mule.context.DefaultMuleContextFactory;
/**
 * 
 * @author longgangbai
 *
 */
public class HttpInvokerServer 
{

	   public static void main(String[] args) {
			 try {
//				  String configFile = "spring-remoting-mule-config-flow.xml";
//				  String configFile = "spring-remoting-mule-config-service.xml";
				 String configFile = "spring-remoting-mule-config.xml";
			      System.setProperty("mule.verbose.exceptions","true");
			      String[] configFileArr = new String[] {configFile };
			      MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
			      MuleContext muleContext = muleContextFactory
			              .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr));
			      muleContext.start();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
}

 

客户端测试:

package com.easyway.httpinvoker;


import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;

import com.easyway.mule.spring.httpinvoker.remoting.ComplexData;
import com.easyway.mule.spring.httpinvoker.remoting.WorkInterface;
/**
 * 
 * @author longgangbai
 *
 */
public class HttpInvokerClient 
{

	private static final String SPRING_HTTP_ENDPOINT = "http://localhost:8003/springService";
    public static void main(String[] args) {
	   ComplexData cd = new ComplexData("Foo", new Integer(13));
       HttpInvokerProxyFactoryBean invoker = new HttpInvokerProxyFactoryBean();
       invoker.setServiceInterface(WorkInterface.class);
       invoker.setServiceUrl(SPRING_HTTP_ENDPOINT);
       invoker.afterPropertiesSet();
       WorkInterface worker = (WorkInterface)invoker.getObject();
       ComplexData data = worker.executeComplexity(cd);
       System.out.println(data.getSomeString());
       System.out.println(data.getSomeInteger());
   }
}

 

你可能感兴趣的:(SOA,mule,ESB)