This gist shows a simple way to create a Mule custom filter which are prototype Spring beans.
Last active
June 15, 2016 18:25
-
-
Save ullgren/edf7e3f44c032833e26a to your computer and use it in GitHub Desktop.
Mule custom filter with a prototype filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mule.api.MuleMessage; | |
import org.mule.api.routing.filter.Filter; | |
import org.springframework.beans.BeansException; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.ApplicationContextAware; | |
public class FilterProxy implements Filter, ApplicationContextAware { | |
private ApplicationContext context; | |
private String realFilterId; | |
@Override | |
public boolean accept(MuleMessage message) { | |
Filter filter = (Filter) context.getBean(realFilterId, Filter.class); | |
return filter.accept(message); | |
} | |
public void setRealFilterId(String realFilterId) { | |
this.realFilterId = realFilterId; | |
} | |
@Override | |
public void setApplicationContext(ApplicationContext context) | |
throws BeansException { | |
this.context = context; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import org.apache.log4j.Logger; | |
import org.mule.api.MuleMessage; | |
import org.mule.api.routing.filter.Filter; | |
public class MyFilter implements Filter { | |
private int randomNumber; | |
Logger log = Logger.getLogger(MyFilter.class); | |
public MyFilter() { | |
Random random = new Random(); | |
randomNumber = random.nextInt(100); | |
} | |
@Override | |
public boolean accept(MuleMessage message) { | |
log.info(this + " has number " + randomNumber); | |
return (randomNumber >= 50); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<mule 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:spring="http://www.springframework.org/schema/beans" version="EE-3.5.0" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
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="realFilter" class="MyFilter" scope="prototype"/> | |
<flow name="simplefilterFlow1" doc:name="simplefilterFlow1"> | |
<http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP"/> | |
<custom-filter class="FilterProxy" doc:name="Custom"> | |
<spring:property name="realFilterId" value="realFilter"/> | |
</custom-filter> | |
<set-payload value="Passed filter" doc:name="Set Payload"/> | |
</flow> | |
</mule> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment