Tuesday, August 9, 2011

Programmically create a Mule 3.x Inbound point to a Mule service

I use JMS inbound endpoint as an example.  Different types of inbound endpoints differ slightly.

1. Create an inbound point configurer of the target inbound type

public class JmsInboundConfigurer implements MuleContextAware
{
    ServiceCompositeMessageSource msgSource;
   
    public void setMuleContext(MuleContext context) {...}
    public MuleContext getMuleContext() {...}
    public void setService(Service svc) {...}
    public Service getService() {...}
    public void setConnector(Connector conner) {...}
    public Connector getConnector() {...}
}

In the mule-config.xml, add

<spring:bean id="my.configurer" class="com.acme.JmsInboundConfigurer" init-method="init">
    <spring:property name="service">
        <spring:ref bean="my.service"/>
    </spring:property>
    <spring:property name="connector">
        <spring:ref bean="activemq.connector"/>
    </spring:property> 
</spring:bean>

2. Statically configure an inbound point and read it back to match the configuration

<jms:activemq-connector name="activemq.connector" 
 brokerURL="tcp://localhost:61616"/>
<jms:endpoint name="jms.endpoint" queue="..."
 connector-ref="activemq.connector" exchange-pattern="request-response"/>

JmsInboundConfigurer.init() would make a good place to output all the information.

this.msgSource = 
(ServiceCompositeMessageSource)getService().getMessageSource();
logger.info(msgSource.toString());

Use msgSource.getEndpoint("jms.endpoint") or msgSource.getEndpoints() to read back end points to compare with the static configuration

3. Comment out the static configure and create it programmatically

The following code fragment creates and add a JMS inbound point to the service assigned

EndpointURIEndpointBuilder builder = new EndpointURIEndpointBuilder(uri, this.muleContext);
builder.setName("jms.endpoint");
builder.setConnector(this.connector);
builder.setExchangePattern("request-response");

// builder.setProperty("", xyz);  // if any
if (transaction needed) {
    MuleTransactionConfig txConf = new MuleTransactionConfig();
    txConf.setAction(ACTION_BEGIN_OR_JOIN);
    txConf.setMuleContext(muleContext);
    txConf.setTimeout(30000);
    JmsTransactionFactory factory = new JmsTransactionFactory();
    factory.setName("a-good-name");
    txConf.setFactory(factory);
    builder.setTransactionConfig(txConf);
}
builder.setInitialState(AbstractService.INITIAL_STATE_STARTED);
builder.setResponseTimeout(10000);
builder.setDeleteUnacceptedMessages(false);
builder.setDisableTransportTransformer(false);
builder.setEncoding("UTF-8");
InboundEndpoint inept = builder.buildInboundEndpoint();
muleContext.getRegistry().registerEndpoint(inept);
msgSource.addSource(inept);

The inbound endpoint should work at this point.

No comments:

Post a Comment