当前位置: 代码迷 >> Ajax >> Mule 3.4.0中对Ajax的运用
  详细解决方案

Mule 3.4.0中对Ajax的运用

热度:693   发布时间:2013-08-26 12:17:40.0
Mule 3.4.0中对Ajax的应用

?

public class DummyAjaxComponent
{
    public DummyJsonBean process(String name)
    {
        return new DummyJsonBean(name);
    }
}

?

?

/**
 * 
 * <p>功能描述,该部分必须以中文句号结尾。<p>
 *
 * 创建日期 2013-8-19<br>
 * @author  $Author$<br>
 * @version $Revision$ $Date$
 * @since   3.0.0
 */
public class DummyAjaxComponent2
{
    public String process(DummyJsonBean bean)
    {
        return "Received: " + bean;
    }
}

?

?

?

import org.codehaus.jackson.annotate.JsonProperty;

/**
 * 
 * <p>功能描述,该部分必须以中文句号结尾。<p>
 *
 * 创建日期 2013-8-19<br>
 * @author  $Author$<br>
 * @version $Revision$ $Date$
 * @since   3.0.0
 */
public class DummyJsonBean
{
    @JsonProperty
    private String name;

    public DummyJsonBean()
    {
    }

    public DummyJsonBean(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    @Override
    public String toString()
    {
        return "DummyJsonBean{" +
                "name='" + name + '\'' +
                '}';
    }
}

?

?

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:ajax="http://www.mulesoft.org/schema/mule/ajax"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:json="http://www.mulesoft.org/schema/mule/json"
      xmlns:test="http://www.mulesoft.org/schema/mule/test"
      xsi:schemaLocation="
      http://www.mulesoft.org/schema/mule/test http://www.mulesoft.org/schema/mule/test/current/mule-test.xsd
      http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
      http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
       http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
       http://www.mulesoft.org/schema/mule/ajax http://www.mulesoft.org/schema/mule/ajax/current/mule-ajax.xsd">

    <ajax:servlet-endpoint name="ajax-endpoint1" channel="/test1"/>
    <ajax:servlet-endpoint name="ajax-endpoint2" channel="/test2"/>

    <model name="main">
        <service name="clientSubscribe-test1">
            <description>This service will send an instance of JsonBean to any clients listening on "/test1" channel</description>
            <inbound>
                <vm:inbound-endpoint path="in1" exchange-pattern="one-way"/>
            </inbound>
            <component class="com.easyway.esb.mule.ajax.DummyAjaxComponent"/>
            <outbound>
                <pass-through-router>
                    <outbound-endpoint ref="ajax-endpoint1"/>
                </pass-through-router>
            </outbound>
        </service>

        <service name="clientPublish-test2">
            <description>This service will receive on "/test2" channel but the request will be converted to a JsonBean instance</description>
            <inbound>
                <inbound-endpoint ref="ajax-endpoint2">
                    <json:json-to-object-transformer returnClass="com.easyway.esb.mule.ajax.DummyJsonBean"/>
                </inbound-endpoint>
            </inbound>
            <component class="com.easyway.esb.mule.ajax.DummyAjaxComponent2"/>
            <outbound>
                <pass-through-router>
                    <vm:outbound-endpoint path="in2" exchange-pattern="one-way"/>
                </pass-through-router>
            </outbound>
        </service>
    </model>
</mule>

?

?

测试:

 public static void main(String[] args)  {
        try {
            Server httpServer = new Server(8090);
            String configFile = "mule-ajax-config.xml";
            String[] configFileArr = new String[] {configFile };
            MuleContextFactory muleContextFactory = new DefaultMuleContextFactory();
            MuleContext context = muleContextFactory
                    .createMuleContext(new SpringXmlConfigurationBuilder(configFileArr));
      
            Context c = new Context(httpServer, "/", Context.SESSIONS);
            c.addServlet(new ServletHolder(new MuleAjaxServlet()), "/ajax/*");
            c.addEventListener(new MuleServletContextListener(context, null));

            httpServer.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }
}

?

  相关解决方案