The example below uses the file transport to access a local file’s content, it then creates a simple xml payload using the content(if the given condition is met) before sending it out to a processing endpoint over HTTP.
This file contains 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 ballerina.lang.messages; | |
import ballerina.lang.system; | |
import ballerina.net.file; | |
import ballerina.net.http; | |
import ballerina.lang.strings; | |
@file:FileSource { | |
fileURI:"file:///home/dumiduh/systedd/log/log", | |
pollingInterval:"10000", | |
deleteIfNotAcknowledged:"false", | |
fileSortAttribute:"size", | |
fileSortAscending:"false" | |
} | |
service fileProcessService { | |
message request = {}; | |
resource processOrder (message m) { | |
http:ClientConnector backend = create http:ClientConnector("http://localhost:9090"); | |
string content = messages:getStringPayload(m); | |
if (strings:contains(content,"X-DSPAM-Probability: 0.0000")){ | |
int t = system:epochTime(); | |
string systime = strings:valueOf(t); | |
xml payload = `<payload><epoch>${systime}</epoch><content>${content}</content></payload>`; | |
messages:setXmlPayload(m,payload); | |
request = http:ClientConnector.post(backend, "/backend", m); | |
} | |
else | |
{ | |
system:log(3,"No X-DSPAM matches found, run complete."); | |
} | |
} | |
} |
Note: to use some of the transports mentioned above you will need to put in a few dependency jars into ballerina libs. Look to the respective transport’s documentation for further instructions.
Note: the example above s is meant to be a crude demonstration of the uniform, almost transport independent programming interface provided by ballerina.
Find out more about ballerina here: http://ballerinalang.org/
Note: this example was written for ballerina 0.87 and may not work with other versions.
No comments:
Post a Comment