Thursday, June 25, 2009

Using Camel to direct messages to a Printer using a camel-printer component

I have recently made a feature submission of a camel-printer component to the Apache Camel community. I believe it will be available in Apache Camel 2.1 release cycle.

You can follow the submission to the Apache community at the following JIRA link

https://issues.apache.org/activemq/browse/CAMEL-1675

The objective of this component is to provide a way to direct payloads on a route to a printer. Obviously the payload has to be a formatted piece of payload in order for the component to appropriately print it. The objective is to be able to direct specific payloads as jobs to a line printer in a camel flow.

The functionality allows for the payload to be printed on a default, local remote or wirelessly linked printer using the javax printing API under the covers.

Since the URI scheme for a printer has not been standardized (the nearest thing to a standard being the IETF print standard) and therefore not uniformly applied by vendors, I have chosen "lpr" as the scheme.

There is choice for setting the following options in the printer URI

Printer options

- mediasize: to set the stationary as defined by
enumeration settings in the
javax.print.attribute.standard.MediaSizeName API.
- copies: to set number of copies based on the
javax.print.attribute.standard.Copies API
- sides: to set one sided or two sided printing based on the
javax.print.attribute.standard.Sides API


The way this camel printer works in Camel is as follows

Example 1: Printing text based payloads on a Default printer using letter stationary and one-sided mode

RouteBuilder builder = new RouteBuilder() {
public void configure() {
from(file://inputdir/?delete=true)
.to("lpr://localhost/default?copies=2" +
"&flavor=DocFlavor.INPUT_STREAM&" +
"&mimeType=AUTOSENSE" +
"&mediaSize=na-letter" +
"&sides=one-sided")
}
};


Example 2: Printing GIF based payloads on a Remote printer using A4 stationary and one-sided mode

RouteBuilder builder = new RouteBuilder() {
public void configure() {
from(file://inputdir/?delete=true)
.to("lpr://remotehost/sales/salesprinter" +
"?copies=2&sides=one-sided" +
"&mimeType=GIF&mediaSize=iso-a4" +
"&flavor=DocFlavor.INPUT_STREAM")
}
};


Example 3: Printing JPEG based payloads on a Remote printer using Japanese Postcard stationary and one-sided mode

RouteBuilder builder = new RouteBuilder() {
public void configure() {
from(file://inputdir/?delete=true)
.to("lpr://remotehost/sales/salesprinter" +
"?copies=2&sides=one-sided" +
"&mimeType=JPEG" +
"&mediaSize=japanese-postcard" +
"&flavor=DocFlavor.INPUT_STREAM")
}
};


I hope you like this feature and use it to direct formatted payloads to a printer as part of a camel flow.