use of facebook4j.Facebook in project camel by apache.
the class FacebookProducer method process.
@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
// properties for method arguments
final Map<String, Object> properties = new HashMap<String, Object>();
getExchangeProperties(exchange, properties);
FacebookPropertiesHelper.configureReadingProperties(endpoint.getConfiguration(), properties);
getEndpointProperties(endpoint.getConfiguration(), properties);
// decide which method to invoke
final FacebookMethodsType method = findMethod(exchange, properties);
if (method == null) {
// synchronous failure
callback.done(true);
return true;
}
// create a runnable invocation task to be submitted on a background thread pool
// this way we avoid blocking the current thread for long running operations
Runnable invocation = new Runnable() {
@Override
public void run() {
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Invoking method {} with {}", method.getName(), properties.keySet());
}
// also check whether we need to get Raw JSON
Object result;
String rawJSON = null;
if (endpoint.getConfiguration().getJsonStoreEnabled() == null || !endpoint.getConfiguration().getJsonStoreEnabled()) {
result = FacebookMethodsTypeHelper.invokeMethod(endpoint.getConfiguration().getFacebook(), method, properties);
} else {
final Facebook facebook = endpoint.getConfiguration().getFacebook();
// lock out the underlying Facebook object from other threads
synchronized (facebook) {
result = FacebookMethodsTypeHelper.invokeMethod(facebook, method, properties);
rawJSON = DataObjectFactory.getRawJSON(result);
}
}
// producer returns a single response, even for methods with List return types
exchange.getOut().setBody(result);
// copy headers
exchange.getOut().setHeaders(exchange.getIn().getHeaders());
if (rawJSON != null) {
exchange.getOut().setHeader(FacebookConstants.FACEBOOK_PROPERTY_PREFIX + "rawJSON", rawJSON);
}
} catch (Throwable t) {
exchange.setException(ObjectHelper.wrapRuntimeCamelException(t));
} finally {
callback.done(false);
}
}
};
getExecutorService(getEndpoint().getCamelContext()).submit(invocation);
return false;
}
use of facebook4j.Facebook in project camel by apache.
the class FacebookConsumer method poll.
@Override
protected int poll() throws Exception {
// invoke the consumer method
final Map<String, Object> args = getMethodArguments();
try {
// also check whether we need to get raw JSON
String rawJSON = null;
Object result;
if (endpoint.getConfiguration().getJsonStoreEnabled() == null || !endpoint.getConfiguration().getJsonStoreEnabled()) {
result = invokeMethod(endpoint.getConfiguration().getFacebook(), method, args);
} else {
final Facebook facebook = endpoint.getConfiguration().getFacebook();
synchronized (facebook) {
result = invokeMethod(facebook, method, args);
rawJSON = DataObjectFactory.getRawJSON(result);
}
}
// process result according to type
if (result != null && (result instanceof Collection || result.getClass().isArray())) {
// create an exchange for every element
final Object array = getResultAsArray(result);
final int length = Array.getLength(array);
for (int i = 0; i < length; i++) {
processResult(Array.get(array, i), rawJSON);
}
return length;
} else {
processResult(result, rawJSON);
// number of messages polled
return 1;
}
} catch (Throwable t) {
throw ObjectHelper.wrapRuntimeCamelException(t);
}
}
Aggregations