use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.
the class WonNodeControllerActor method onReceive.
/**
* Receive messages about newly discovered won node and decide to crawl or skip
* processing these won nodes.
*
* @param message
*/
@Override
public void onReceive(final Object message) {
if (message instanceof Terminated) {
// if it is some other actor handle it differently
handleConnectionErrors((Terminated) message);
return;
}
if (message.equals(LIFE_CHECK_TICK)) {
lifeCheck();
return;
}
if (message instanceof WonNodeEvent) {
WonNodeEvent event = (WonNodeEvent) message;
if (event.getStatus().equals(WonNodeEvent.STATUS.NEW_WON_NODE_DISCOVERED) || event.getStatus().equals(WonNodeEvent.STATUS.GET_WON_NODE_INFO_FOR_CRAWLING) || event.getStatus().equals(WonNodeEvent.STATUS.RETRY_REGISTER_FAILED_WON_NODE)) {
// won node has already been discovered and connected
if (crawlWonNodes.containsKey(event.getWonNodeUri())) {
log.debug("Won node uri '{}' already discovered", event.getWonNodeUri());
if (event.getStatus().equals(WonNodeEvent.STATUS.GET_WON_NODE_INFO_FOR_CRAWLING)) {
WonNodeInfo wonNodeInfo = crawlWonNodes.get(event.getWonNodeUri()).getWonNodeInfo();
WonNodeEvent e = new WonNodeEvent(event.getWonNodeUri(), WonNodeEvent.STATUS.CONNECTED_TO_WON_NODE, wonNodeInfo);
pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
}
return;
}
// skip crawling of won nodes in the skip list
if (skipWonNodeUris.contains(event.getWonNodeUri())) {
log.debug("Skip crawling won node with uri '{}'", event.getWonNodeUri());
WonNodeEvent e = new WonNodeEvent(event.getWonNodeUri(), WonNodeEvent.STATUS.SKIP_WON_NODE);
pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
return;
}
// shall we try to connect to the won node or has it failed already ?
if (failedWonNodeUris.contains(event.getWonNodeUri())) {
log.debug("Suppress connection to already failed won node with uri {} , will try to connect later ...", event.getWonNodeUri());
return;
}
// try the connect to won node
boolean logRegisterWarningForWonNode = event.getStatus().equals(WonNodeEvent.STATUS.RETRY_REGISTER_FAILED_WON_NODE);
WonNodeConnection wonNodeConnection = addWonNodeForCrawling(event.getWonNodeUri(), logRegisterWarningForWonNode);
// connection failed ?
if (failedWonNodeUris.contains(event.getWonNodeUri())) {
log.debug("Still could not connect to won node with uri: {}, will retry later ...", event.getWonNodeUri());
return;
}
// tell the crawler about discovered won nodes
if (wonNodeConnection == null || wonNodeConnection.getWonNodeInfo() == null) {
log.error("Cannot retrieve won node info from won node connection!");
return;
}
WonNodeEvent e = new WonNodeEvent(event.getWonNodeUri(), WonNodeEvent.STATUS.CONNECTED_TO_WON_NODE, wonNodeConnection.getWonNodeInfo());
pubSubMediator.tell(new DistributedPubSubMediator.Publish(e.getClass().getName(), e), getSelf());
return;
}
}
// send back hints to won nodes
if (message instanceof HintEvent) {
processHint((HintEvent) message);
return;
} else if (message instanceof BulkHintEvent) {
BulkHintEvent bulkHintEvent = (BulkHintEvent) message;
for (HintEvent hint : bulkHintEvent.getHintEvents()) {
expandToSocketHintsIfAppropriate(hint).forEach(h -> processHint(h));
}
return;
}
unhandled(message);
}
use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.
the class WonNodeControllerActor method sendHint.
/**
* Send hint event out to won node
*
* @param hint
*/
private void sendHint(HintEvent hint) {
if (!crawlWonNodes.containsKey(hint.getRecipientWonNodeUri())) {
log.warning("cannot send hint to won node {}! Is registered with the won node controller?", hint.getRecipientWonNodeUri());
return;
}
// send hint to first won node
WonNodeConnection fromWonNodeConnection = crawlWonNodes.get(hint.getRecipientWonNodeUri());
log.info("Send hint {} to won node {}", hint, hint.getRecipientWonNodeUri());
fromWonNodeConnection.getHintProducer().tell(hint, getSelf());
}
use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.
the class WonNodeControllerActor method addWonNodeForCrawling.
/**
* Try to register at won nodes and add them for crawling
*
* @param wonNodeUri URI of the won node meta data resource
* @param logWonNodeRegisterWarning if true then log the failed register
* attempts as warning, otherwise as debug level
* @return won node connection if successfully connected, otherwise null
*/
private WonNodeConnection addWonNodeForCrawling(String wonNodeUri, boolean logWonNodeRegisterWarning) {
WonNodeConnection con = null;
Dataset ds = null;
WonNodeInfo nodeInfo = null;
// try register at won node
try {
registrationClient.register(wonNodeUri);
ds = linkedDataSource.getDataForPublicResource(URI.create(wonNodeUri));
} catch (Exception e) {
addFailedWonNode(wonNodeUri, con);
if (logWonNodeRegisterWarning) {
log.warning("Error requesting won node information from {}", wonNodeUri);
log.warning("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
} else {
log.debug("Error requesting won node information from {}", wonNodeUri);
log.debug("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
}
return null;
}
// try save won node info in local rdf store
try {
sparqlService.updateNamedGraphsOfDataset(ds);
nodeInfo = sparqlService.getWonNodeInfoFromDataset(ds);
} catch (Exception e) {
addFailedWonNode(wonNodeUri, con);
log.error("Error saving won node information from {} into RDF store with SPARQL endpoint {}", wonNodeUri, sparqlService.getSparqlEndpoint());
log.error("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
return null;
}
// try subscribe atom updates at won node
try {
con = subscribeAtomUpdates(nodeInfo);
crawlWonNodes.put(nodeInfo.getWonNodeURI(), con);
failedWonNodeUris.remove(nodeInfo.getWonNodeURI());
log.info("registered won node {} and start crawling it", nodeInfo.getWonNodeURI());
} catch (Exception e) {
addFailedWonNode(wonNodeUri, con);
log.error("Error subscribing for atom updates at won node {}", wonNodeUri);
log.error("Exception message: {} \nCause: {} ", e.getMessage(), e.getCause());
}
return con;
}
use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.
the class ActiveMqWonNodeConnectionFactory method createWonNodeConnection.
/**
* Create a {@link won.matcher.service.nodemanager.pojo.WonNodeConnection} for
* active mq
*
* @param context actor context to create the message consuming actors in
* @param wonNodeInfo info about the won node (e.g. topics to subscribe)
* @return the connection
* @throws FailedToCreateConsumerException
*/
public static WonNodeConnection createWonNodeConnection(UntypedActorContext context, WonNodeInfo wonNodeInfo, MessagingContext messagingContext) {
// read won node info
String activeMq = WON.WonOverActiveMq.toString();
String brokerUri = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.brokerUri.toString());
String createdTopic = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.atomCreatedTopic.toString());
String activatedTopic = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.atomActivatedTopic.toString());
String deactivatedTopic = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.atomDeactivatedTopic.toString());
String hintQueue = wonNodeInfo.getSupportedProtocolImplParamValue(activeMq, WON.matcherQueue.toString());
// create the activemq component for this won node
String uuid = UUID.randomUUID().toString();
String componentName = "activemq-" + uuid;
ActiveMQConnectionFactory connectionFactory = createConnectionFactory(brokerUri, messagingContext);
// connectionFactory.setExceptionListener( ... )
Camel camel = CamelExtension.get(context.system());
camel.context().addComponent(componentName, JmsComponent.jmsComponent(connectionFactory));
// create the actors that receive the messages (atom events)
String createdComponent = componentName + ":topic:" + createdTopic + "?testConnectionOnStartup=false&transacted=false";
Props createdProps = SpringExtension.SpringExtProvider.get(context.system()).props(AtomConsumerProtocolActor.class, createdComponent);
ActorRef created = context.actorOf(createdProps, "ActiveMqAtomCreatedConsumerProtocolActor-" + uuid);
logger.info("Create camel component JMS listener {} for won node {}", createdComponent, wonNodeInfo.getWonNodeURI());
ActorRef activated = created;
if (!activatedTopic.equals(createdTopic)) {
String activatedComponent = componentName + ":topic:" + activatedTopic + "?testConnectionOnStartup=false&transacted=false";
Props activatedProps = SpringExtension.SpringExtProvider.get(context.system()).props(AtomConsumerProtocolActor.class, activatedComponent);
activated = context.actorOf(activatedProps, "ActiveMqAtomActivatedConsumerProtocolActor-" + uuid);
logger.info("Create camel component JMS listener {} for won node {}", activatedComponent, wonNodeInfo.getWonNodeURI());
}
ActorRef deactivated;
if (deactivatedTopic.equals(createdTopic)) {
deactivated = created;
} else if (deactivatedTopic.equals(activatedTopic)) {
deactivated = activated;
} else {
String deactivatedComponent = componentName + ":topic:" + deactivatedTopic + "?testConnectionOnStartup=false&transacted=false";
Props deactivatedProps = SpringExtension.SpringExtProvider.get(context.system()).props(AtomConsumerProtocolActor.class, deactivatedComponent);
deactivated = context.actorOf(deactivatedProps, "ActiveMqAtomDeactivatedConsumerProtocolActor-" + uuid);
logger.info("Create camel component JMS listener {} for won node {}", deactivatedComponent, wonNodeInfo.getWonNodeURI());
}
// create the actor that sends messages (hint events)
String hintComponent = componentName + ":queue:" + hintQueue + "?transacted=false";
Props hintProps = SpringExtension.SpringExtProvider.get(context.system()).props(HintProducerProtocolActor.class, hintComponent, null);
ActorRef hintProducer = context.actorOf(hintProps, "ActiveMqHintProducerProtocolActor-" + uuid);
logger.info("Create camel component JMS listener {} for won node {}", hintComponent, wonNodeInfo.getWonNodeURI());
// watch the created consumers from the context to get informed when they are
// terminated
context.watch(created);
context.watch(activated);
context.watch(deactivated);
context.watch(hintProducer);
// create the connection
WonNodeConnection jmsConnection = new WonNodeConnection(wonNodeInfo, created, activated, deactivated, hintProducer);
return jmsConnection;
}
use of won.matcher.service.nodemanager.pojo.WonNodeConnection in project webofneeds by researchstudio-sat.
the class WonNodeControllerActor method handleConnectionErrors.
/**
* Handles connections errors that occur when the atom consumer actors are
* terminated.
*
* @param t messages that holds a reference to consumer actor that was
* terminated
*/
private void handleConnectionErrors(Terminated t) {
for (String uri : crawlWonNodes.keySet()) {
WonNodeConnection con = crawlWonNodes.get(uri);
if (con != null) {
if (con.getAtomCreatedConsumer().equals(t.getActor())) {
log.error("AtomCreatedConsumer '{}' of won '{}' has been shut down", t.getActor(), uri);
addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
} else if (con.getAtomActivatedConsumer().equals(t.getActor())) {
log.error("AtomActivatedConsumer '{}' of won '{}' has been shut down", t.getActor(), uri);
addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
} else if (con.getAtomDeactivatedConsumer().equals(t.getActor())) {
log.error("AtomDeactivatedConsumer '{}' of won '{}' has been shut down", t.getActor(), uri);
addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
} else if (con.getHintProducer().equals(t.getActor())) {
log.error("HintProducer '{}' of won '{}' has been shut down", t.getActor(), uri);
addFailedWonNode(con.getWonNodeInfo().getWonNodeURI(), con);
}
}
}
}
Aggregations