Search in sources :

Example 1 with CamelMessage

use of akka.camel.CamelMessage in project webofneeds by researchstudio-sat.

the class NeedConsumerProtocolActor method onReceive.

@Override
public void onReceive(final Object message) throws Exception {
    if (message instanceof CamelMessage) {
        CamelMessage camelMsg = (CamelMessage) message;
        String needUri = (String) camelMsg.getHeaders().get(MSG_HEADER_NEED_URI);
        String wonNodeUri = (String) camelMsg.getHeaders().get(MSG_HEADER_WON_NODE_URI);
        // monitoring code
        monitoringService.startClock(MonitoringService.NEED_HINT_STOPWATCH, needUri);
        // process the incoming need event
        if (needUri != null && wonNodeUri != null) {
            Object methodName = camelMsg.getHeaders().get(MSG_HEADER_METHODNAME);
            if (methodName != null) {
                log.debug("Received event '{}' for needUri '{}' and wonNeedUri '{}' and publish it to matchers", methodName, needUri, wonNodeUri);
                // publish a need event to all the (distributed) matchers
                NeedEvent event = null;
                long crawlDate = System.currentTimeMillis();
                if (methodName.equals(MSG_HEADER_METHODNAME_NEEDCREATED)) {
                    event = new NeedEvent(needUri, wonNodeUri, NeedEvent.TYPE.ACTIVE, crawlDate, camelMsg.body().toString(), Lang.TRIG);
                    pubSubMediator.tell(new DistributedPubSubMediator.Publish(event.getClass().getName(), event), getSelf());
                } else if (methodName.equals(MSG_HEADER_METHODNAME_NEEDACTIVATED)) {
                    event = new NeedEvent(needUri, wonNodeUri, NeedEvent.TYPE.ACTIVE, crawlDate, camelMsg.body().toString(), Lang.TRIG);
                    pubSubMediator.tell(new DistributedPubSubMediator.Publish(event.getClass().getName(), event), getSelf());
                } else if (methodName.equals(MSG_HEADER_METHODNAME_NEEDDEACTIVATED)) {
                    event = new NeedEvent(needUri, wonNodeUri, NeedEvent.TYPE.INACTIVE, crawlDate, camelMsg.body().toString(), Lang.TRIG);
                    pubSubMediator.tell(new DistributedPubSubMediator.Publish(event.getClass().getName(), event), getSelf());
                } else {
                    unhandled(message);
                }
                // let the crawler save the data of this event too
                ResourceCrawlUriMessage resMsg = new ResourceCrawlUriMessage(needUri, needUri, wonNodeUri, CrawlUriMessage.STATUS.SAVE, crawlDate, null);
                resMsg.setSerializedResource(camelMsg.body().toString());
                resMsg.setSerializationFormat(Lang.TRIG);
                pubSubMediator.tell(new DistributedPubSubMediator.Publish(resMsg.getClass().getName(), resMsg), getSelf());
                return;
            }
        }
    }
    unhandled(message);
}
Also used : DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) NeedEvent(won.matcher.service.common.event.NeedEvent) CamelMessage(akka.camel.CamelMessage) ResourceCrawlUriMessage(won.matcher.service.crawler.msg.ResourceCrawlUriMessage)

Example 2 with CamelMessage

use of akka.camel.CamelMessage in project webofneeds by researchstudio-sat.

the class HintProducerProtocolActor method onTransformOutgoingMessage.

/**
 * transform hint events to camel messages that can be sent to the won node
 *
 * @param message supposed to be a {@link HintEvent}
 * @return
 */
@Override
public Object onTransformOutgoingMessage(Object message) {
    HintEvent hint = (HintEvent) message;
    Map<String, Object> headers = new HashMap<>();
    headers.put("needURI", hint.getFromNeedUri());
    headers.put("otherNeedURI", hint.getToNeedUri());
    headers.put("score", String.valueOf(hint.getScore()));
    headers.put("originator", hint.getMatcherUri());
    // headers.put("content", RdfUtils.toString(hint.deserializeExplanationModel()));
    // headers.put("remoteBrokerEndpoint", localBrokerUri);
    headers.put("methodName", "hint");
    WonMessage wonMessage = createHintWonMessage(hint);
    Object body = WonMessageEncoder.encode(wonMessage, Lang.TRIG);
    CamelMessage camelMsg = new CamelMessage(body, headers);
    // monitoring code
    monitoringService.stopClock(MonitoringService.NEED_HINT_STOPWATCH, hint.getFromNeedUri());
    log.debug("Send hint camel message {}", hint.getFromNeedUri());
    return camelMsg;
}
Also used : HashMap(java.util.HashMap) WonMessage(won.protocol.message.WonMessage) CamelMessage(akka.camel.CamelMessage) HintEvent(won.matcher.service.common.event.HintEvent)

Aggregations

CamelMessage (akka.camel.CamelMessage)2 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)1 HashMap (java.util.HashMap)1 HintEvent (won.matcher.service.common.event.HintEvent)1 NeedEvent (won.matcher.service.common.event.NeedEvent)1 ResourceCrawlUriMessage (won.matcher.service.crawler.msg.ResourceCrawlUriMessage)1 WonMessage (won.protocol.message.WonMessage)1