Part 1: Building the Message-Driven EJBCreate a Message-Driven that listens to the topic/wdkDocument topic: Find example source below: Bloco de código |
---|
theme | Eclipse |
---|
language | java |
---|
title | TxtMessageBean.java |
---|
firstline | 1 |
---|
linenumbers | true |
---|
| package com.fluig.conv;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.Message;
import javax.jms.MessageListener;
@MessageDriven(name = "test/TxtConverter", activationConfig = {
@ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Topic"),
@ActivationConfigProperty(propertyName = "destination", propertyValue = "topic/wdkDocument")
})
public class TxtMessageBean implements MessageListener {
@Override
public void onMessage(Message message) {
try {
String event = message.getStringProperty("event");
if (event != null && event.equals("EXTERNALCONVERTION")) {
String doc = message.getStringProperty("documentpath");
if (doc != null && doc.endsWith(".txt")) {
ProcessBuilder pb = new ProcessBuilder("txt2pdf.exe", doc, doc + ".pdf", "-lpp40");
pb.start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} |
The Message-Driven above configures the destinationType property for "javax.jms.Topic" to indicate that it will listen to a JMS topic, and the destination property to"topic/wdkDocument" to indicate which topic will be listened to. The "topic/wdkDocument" topic is where the document publishing events are sent to. Upon receiving a message, the type of published event must be checked through the StringProperty "event"; if the value of this property is "EXTERNALCONVERTION", it means that Fluig is indicating that this is the time for the customized conversion to take place. In order to obtain the document path, check the StringProperty "documentpath". After this, the file extension must be checked to validate if this is the correct converter, because there may be more than one converter for different extensions. Once the parameters are validated, the converter program can be called; in the example, the txt2pdf http://www.verypdf.com/txt2pdf/ freeware was used. The target document must have the same name plus the .pdf extension, and it should be in the same directory. Ex: doc.txt converted: doc.txt.pdf It is important to remember that the source document may not be modified, with the penalty of Fluig indicating that it has been externally changed (CRC validation). This Message-Driven must be packed and deployed on the application server according to Java™ EE specifications. |