Apache Camel is a rule-based routing and mediation engine providing implementation of Enterprise Integration Patterns. It allows message transfer from different sources to different destinations (see http://camel.apache.org/components.html for further details).
Playing around with Apache Camel i’ve decided to write a simple example sending push notifications to the iOS devices.
One option is to use original Camel’s Apns Component. This requires however your APNS provider has to be registered by Apple in order to send push notifications using generated certificate.
Another option is to use a free, public available APNS provider. In this example I’ll use Boxcar.IO as an APNS provider.
Boxcar allows you to create your own provider (= boxcar account) and send push notifications through it to the free Boxcar App installed on your iOS device. Alternatively (for testing or development purpose) one of the pre-configured, generic providers can be used (i’ll use generic “Monitoring” provider in this post).
Furthermore Boxcar exposes a very simple REST API containing only 3 RESTful requests: SUBSCRIBE, CREATE and BROADCAST. SUBSCRIBE will add your service for a user. CREATE sends a message to one user, or a subset of your users. BROADCAST sends a single message to all of your users.
Let’s prepare the iOS device for using Boxcar first.
curl -d "[email protected]" http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications/subscribe
curl -d "[email protected]¬ification[from_screen_name]=Hello¬ification[message]=This+is+an+example" http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications
Now we are able to send push notifications to our iOS device through Boxcar.
Next step is to create Apache Camel routes by implementing following scenario:
We’ll start with the Maven dependencies required to fulfill the task above:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-core</artifactId> <version>2.10.1</version> </dependency> <dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-http</artifactId> <version>2.10.1</version> </dependency>
Next we have to define our 2 route by subclassing Camel’s RouteBuilder:
public class BoxcarRouteBuilder extends RouteBuilder { private static final String BOXCAR_QUERY_PREFIX = "[email protected]¬ification[from_screen_name]=Javaforge Blog¬ification[message]="; @Override public void configure() throws Exception { from("jetty:http://0.0.0.0:8888/messages") .setBody(simple(BOXCAR_QUERY_PREFIX + "${in.headers.msg}")) .to("log:net.javaforge.blog.camel.LOG") .to("http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications?bridgeEndpoint=true") .transform().constant("OK"); from("file:target/inbox?noop=true") .setBody(simple(BOXCAR_QUERY_PREFIX + "File added: ${file:path}")) .to("log:net.javaforge.blog.camel.LOG") .to("http://boxcar.io/devices/providers/MH0S7xOFSwVLNvNhTpiC/notifications"); } }
Some explanations:
Last step is to start Apache Camel in standalone mode and to provide BoxcarRouteBuilder to it:
public class BoxcarRunner { public static void main(String[] args) throws Exception { Main main = new Main(); // enable hangup support so you can press ctrl + c to terminate the JVM main.enableHangupSupport(); // add routes main.addRouteBuilder(new BoxcarRouteBuilder()); // run until you terminate the JVM System.out.println("Starting Camel. Use ctrl + c to terminate the JVM.\n"); main.run(); } }
Now we can either drop some files into the “target/inbox” folder or send HTTP requests to the localhost:8888/messages like this:
curl -d "msg=Hello Iphone" http://localhost:8888/messages
Here are some screenshots taken on iPhone after it was bombed with the push notifications :)
tested! Working! Thanks!