com.devebot.opflow.OpflowRpcWorker Maven / Gradle / Ivy
package com.devebot.opflow;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author drupalex
*/
public class OpflowRpcWorker {
final Logger logger = LoggerFactory.getLogger(OpflowRpcWorker.class);
private final OpflowBroker broker;
private final String operatorName;
private final String responseName;
public OpflowRpcWorker(Map params) throws Exception {
Map brokerParams = new HashMap();
brokerParams.put("mode", "rpc.worker");
brokerParams.put("uri", params.get("uri"));
brokerParams.put("exchangeName", params.get("exchangeName"));
brokerParams.put("exchangeType", "direct");
brokerParams.put("routingKey", params.get("routingKey"));
broker = new OpflowBroker(brokerParams);
operatorName = (String) params.get("operatorName");
responseName = (String) params.get("responseName");
}
private OpflowBroker.ConsumerInfo consumerInfo;
private List middlewares = new LinkedList();
public OpflowBroker.ConsumerInfo process(final OpflowRpcListener listener) {
return process(TRUE, listener);
}
public OpflowBroker.ConsumerInfo process(final String routineId, final OpflowRpcListener listener) {
return process(new Checker() {
@Override
public boolean match(String originRoutineId) {
return routineId != null && routineId.equals(originRoutineId);
}
}, listener);
};
public OpflowBroker.ConsumerInfo process(final String[] routineIds, final OpflowRpcListener listener) {
return process(new Checker() {
@Override
public boolean match(String originRoutineId) {
return routineIds != null && OpflowUtil.arrayContains(routineIds, originRoutineId);
}
}, listener);
};
public OpflowBroker.ConsumerInfo process(Checker checker, final OpflowRpcListener listener) {
if (checker != null && listener != null) {
middlewares.add(new Middleware(checker, listener));
}
if (consumerInfo != null) return consumerInfo;
return consumerInfo = broker.consume(new OpflowListener() {
@Override
public void processMessage(byte[] content, AMQP.BasicProperties properties, String queueName, Channel channel) throws IOException {
OpflowRpcResponse response = new OpflowRpcResponse(channel, properties, queueName);
Map headers = OpflowUtil.getHeaders(properties);
String routineId = null;
if (headers.get("routineId") != null) {
routineId = headers.get("routineId").toString();
}
for(Middleware middleware : middlewares) {
if (middleware.getChecker().match(routineId)) {
Boolean nextAction = middleware.getListener().processMessage(new OpflowMessage(content, properties.getHeaders()), response);
if (nextAction == null || nextAction == OpflowRpcListener.DONE) break;
}
}
}
}, OpflowUtil.buildOptions(new OpflowUtil.MapListener() {
@Override
public void transform(Map opts) {
opts.put("queueName", operatorName);
opts.put("replyTo", responseName);
opts.put("binding", Boolean.TRUE);
}
}));
}
public void close() {
if (broker != null) broker.close();
}
public class Middleware {
private Checker checker;
private OpflowRpcListener listener;
public Middleware(Checker checker, OpflowRpcListener listener) {
this.checker = checker;
this.listener = listener;
}
public Checker getChecker() {
return checker;
}
public OpflowRpcListener getListener() {
return listener;
}
}
public interface Checker {
public boolean match(String routineId);
}
private final Checker TRUE = new Checker() {
@Override
public boolean match(String routineId) {
return true;
}
};
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy