use of org.apache.camel.Exchange in project camel by apache.
the class GridFsConsumer method run.
@Override
public void run() {
DBCursor c = null;
java.util.Date fromDate = null;
QueryStrategy s = endpoint.getQueryStrategy();
boolean usesTimestamp = s != QueryStrategy.FileAttribute;
boolean persistsTimestamp = s == QueryStrategy.PersistentTimestamp || s == QueryStrategy.PersistentTimestampAndFileAttribute;
boolean usesAttribute = s == QueryStrategy.FileAttribute || s == QueryStrategy.TimeStampAndFileAttribute || s == QueryStrategy.PersistentTimestampAndFileAttribute;
DBCollection ptsCollection = null;
DBObject persistentTimestamp = null;
if (persistsTimestamp) {
ptsCollection = endpoint.getDB().getCollection(endpoint.getPersistentTSCollection());
// ensure standard indexes as long as collections are small
try {
if (ptsCollection.count() < 1000) {
ptsCollection.createIndex(new BasicDBObject("id", 1));
}
} catch (MongoException e) {
//TODO: Logging
}
persistentTimestamp = ptsCollection.findOne(new BasicDBObject("id", endpoint.getPersistentTSObject()));
if (persistentTimestamp == null) {
persistentTimestamp = new BasicDBObject("id", endpoint.getPersistentTSObject());
fromDate = new java.util.Date();
persistentTimestamp.put("timestamp", fromDate);
ptsCollection.save(persistentTimestamp);
}
fromDate = (java.util.Date) persistentTimestamp.get("timestamp");
} else if (usesTimestamp) {
fromDate = new java.util.Date();
}
try {
Thread.sleep(endpoint.getInitialDelay());
while (isStarted()) {
if (c == null || c.getCursorId() == 0) {
if (c != null) {
c.close();
}
String queryString = endpoint.getQuery();
DBObject query;
if (queryString == null) {
query = new BasicDBObject();
} else {
query = (DBObject) JSON.parse(queryString);
}
if (usesTimestamp) {
query.put("uploadDate", new BasicDBObject("$gt", fromDate));
}
if (usesAttribute) {
query.put(endpoint.getFileAttributeName(), null);
}
c = endpoint.getFilesCollection().find(query);
}
boolean dateModified = false;
while (c.hasNext() && isStarted()) {
GridFSDBFile file = (GridFSDBFile) c.next();
GridFSDBFile forig = file;
if (usesAttribute) {
file.put(endpoint.getFileAttributeName(), "processing");
DBObject q = BasicDBObjectBuilder.start("_id", file.getId()).append("camel-processed", null).get();
forig = (GridFSDBFile) endpoint.getFilesCollection().findAndModify(q, null, null, false, file, true, false);
}
if (forig != null) {
file = endpoint.getGridFs().findOne(new BasicDBObject("_id", file.getId()));
Exchange exchange = endpoint.createExchange();
exchange.getIn().setHeader(GridFsEndpoint.GRIDFS_METADATA, JSON.serialize(file.getMetaData()));
exchange.getIn().setHeader(Exchange.FILE_CONTENT_TYPE, file.getContentType());
exchange.getIn().setHeader(Exchange.FILE_LENGTH, file.getLength());
exchange.getIn().setHeader(Exchange.FILE_LAST_MODIFIED, file.getUploadDate());
exchange.getIn().setBody(file.getInputStream(), InputStream.class);
try {
getProcessor().process(exchange);
//System.out.println("Processing " + file.getFilename());
if (usesAttribute) {
forig.put(endpoint.getFileAttributeName(), "done");
endpoint.getFilesCollection().save(forig);
}
if (usesTimestamp) {
if (file.getUploadDate().compareTo(fromDate) > 0) {
fromDate = file.getUploadDate();
dateModified = true;
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
if (persistsTimestamp && dateModified) {
persistentTimestamp.put("timestamp", fromDate);
ptsCollection.save(persistentTimestamp);
}
Thread.sleep(endpoint.getDelay());
}
} catch (Throwable e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (c != null) {
c.close();
}
}
use of org.apache.camel.Exchange in project camel by apache.
the class MongoDbEndpoint method createMongoDbExchange.
public Exchange createMongoDbExchange(DBObject dbObj) {
Exchange exchange = super.createExchange();
Message message = exchange.getIn();
message.setHeader(MongoDbConstants.DATABASE, database);
message.setHeader(MongoDbConstants.COLLECTION, collection);
message.setHeader(MongoDbConstants.FROM_TAILABLE, true);
message.setBody(dbObj);
return exchange;
}
use of org.apache.camel.Exchange in project camel by apache.
the class RssEndpoint method createExchange.
@Override
public Exchange createExchange(Object feed, Object entry) {
Exchange exchange = createExchangeWithFeedHeader(feed, RssConstants.RSS_FEED);
SyndFeed newFeed;
try {
newFeed = (SyndFeed) ((SyndFeed) feed).clone();
newFeed.setEntries(Arrays.asList(entry));
} catch (CloneNotSupportedException e) {
LOG.debug("Could not create a new feed. This exception will be ignored.", e);
newFeed = null;
}
exchange.getIn().setBody(newFeed);
return exchange;
}
use of org.apache.camel.Exchange in project Activiti by Activiti.
the class InitiatorCamelCallTest method testInitiatorCamelCall.
@Deployment
public void testInitiatorCamelCall() throws Exception {
CamelContext ctx = applicationContext.getBean(CamelContext.class);
ProducerTemplate tpl = ctx.createProducerTemplate();
String body = "body text";
Exchange exchange = ctx.getEndpoint("direct:startWithInitiatorHeader").createExchange();
exchange.getIn().setBody(body);
tpl.send("direct:startWithInitiatorHeader", exchange);
String instanceId = (String) exchange.getProperty("PROCESS_ID_PROPERTY");
String initiator = (String) runtimeService.getVariable(instanceId, "initiator");
assertEquals("kermit", initiator);
Object camelInitiatorHeader = runtimeService.getVariable(instanceId, "CamelProcessInitiatorHeader");
assertNull(camelInitiatorHeader);
}
use of org.apache.camel.Exchange in project Activiti by Activiti.
the class CamelVariableTransferTest method testCamelPropertiesAll.
// check that at least all properties are passed from camel to activiti when copyVariablesFromProperties=true is simply true
@Deployment
public void testCamelPropertiesAll() throws Exception {
ProducerTemplate tpl = camelContext.createProducerTemplate();
Exchange exchange = camelContext.getEndpoint("direct:startAllProperties").createExchange();
tpl.send("direct:startAllProperties", exchange);
assertNotNull(taskService);
assertNotNull(runtimeService);
assertEquals(1, taskService.createTaskQuery().count());
Task task = taskService.createTaskQuery().singleResult();
assertNotNull(task);
Map<String, Object> variables = runtimeService.getVariables(task.getExecutionId());
assertEquals("sampleValueForProperty1", variables.get("property1"));
assertEquals("sampleValueForProperty2", variables.get("property2"));
assertEquals("sampleValueForProperty3", variables.get("property3"));
}
Aggregations