use of org.apache.rya.periodic.notification.notification.PeriodicNotification in project incubator-rya by apache.
the class CommandNotificationTypeAdapter method serialize.
@Override
public JsonElement serialize(CommandNotification arg0, Type arg1, JsonSerializationContext arg2) {
JsonObject result = new JsonObject();
result.add("command", new JsonPrimitive(arg0.getCommand().name()));
Notification notification = arg0.getNotification();
if (notification instanceof PeriodicNotification) {
result.add("type", new JsonPrimitive(PeriodicNotification.class.getSimpleName()));
PeriodicNotificationTypeAdapter adapter = new PeriodicNotificationTypeAdapter();
result.add("notification", adapter.serialize((PeriodicNotification) notification, PeriodicNotification.class, arg2));
} else if (notification instanceof BasicNotification) {
result.add("type", new JsonPrimitive(BasicNotification.class.getSimpleName()));
BasicNotificationTypeAdapter adapter = new BasicNotificationTypeAdapter();
result.add("notification", adapter.serialize((BasicNotification) notification, BasicNotification.class, arg2));
} else {
throw new IllegalArgumentException("Invalid notification type.");
}
return result;
}
use of org.apache.rya.periodic.notification.notification.PeriodicNotification in project incubator-rya by apache.
the class CreatePeriodicQuery method createPeriodicQuery.
/**
* Creates a Periodic Query by adding the query to Fluo and using the resulting
* Fluo id to create a {@link PeriodicQueryResultStorage} table. Additionally,
* the associated PeriodicNotification is registered with the Periodic Query Service.
*
* @param sparql - sparql query registered to Fluo whose results are stored in PeriodicQueryResultStorage table
* @param notificationClient - {@link PeriodicNotificationClient} for registering new PeriodicNotifications
* @return FluoQuery indicating the metadata of the registered SPARQL query
*/
public FluoQuery createPeriodicQuery(String sparql, PeriodicNotificationClient notificationClient) throws PeriodicQueryCreationException {
try {
Optional<PeriodicQueryNode> optNode = PeriodicQueryUtil.getPeriodicNode(sparql);
if (optNode.isPresent()) {
PeriodicQueryNode periodicNode = optNode.get();
String pcjId = FluoQueryUtils.createNewPcjId();
// register query with Fluo
CreateFluoPcj createPcj = new CreateFluoPcj();
FluoQuery fluoQuery = createPcj.createPcj(pcjId, sparql, Sets.newHashSet(ExportStrategy.PERIODIC), fluoClient);
// register query with PeriodicResultStorage table
periodicStorage.createPeriodicQuery(pcjId, sparql);
// create notification
PeriodicNotification notification = PeriodicNotification.builder().id(pcjId).period(periodicNode.getPeriod()).timeUnit(periodicNode.getUnit()).build();
// register notification with periodic notification app
notificationClient.addNotification(notification);
return fluoQuery;
} else {
throw new RuntimeException("Invalid PeriodicQuery. Query must possess a PeriodicQuery Filter.");
}
} catch (MalformedQueryException | PeriodicQueryStorageException | UnsupportedQueryException e) {
throw new PeriodicQueryCreationException(e);
}
}
use of org.apache.rya.periodic.notification.notification.PeriodicNotification in project incubator-rya by apache.
the class CreatePeriodicQuery method withRyaIntegration.
/**
* Creates a Periodic Query by adding the query to Fluo and using the resulting
* Fluo id to create a {@link PeriodicQueryResultStorage} table.
* @param sparql - sparql query registered to Fluo whose results are stored in PeriodicQueryResultStorage table
* @param notificationClient - {@link PeriodicNotificationClient} for registering new PeriodicNotifications
* @param conn - Accumulo connector for connecting to the Rya instance
* @param ryaInstance - name of the Accumulo back Rya instance
* @return FluoQuery indicating the metadata of the registered SPARQL query
*/
public FluoQuery withRyaIntegration(String sparql, PeriodicNotificationClient notificationClient, Connector conn, String ryaInstance) throws PeriodicQueryCreationException {
try {
Optional<PeriodicQueryNode> optNode = PeriodicQueryUtil.getPeriodicNode(sparql);
if (optNode.isPresent()) {
PeriodicQueryNode periodicNode = optNode.get();
String pcjId = FluoQueryUtils.createNewPcjId();
// register query with Fluo
CreateFluoPcj createPcj = new CreateFluoPcj();
FluoQuery fluoQuery = createPcj.withRyaIntegration(pcjId, sparql, Sets.newHashSet(ExportStrategy.PERIODIC), fluoClient, conn, ryaInstance);
// register query with PeriodicResultStorage table
periodicStorage.createPeriodicQuery(pcjId, sparql);
// create notification
PeriodicNotification notification = PeriodicNotification.builder().id(pcjId).period(periodicNode.getPeriod()).timeUnit(periodicNode.getUnit()).build();
// register notification with periodic notification app
notificationClient.addNotification(notification);
return fluoQuery;
} else {
throw new RuntimeException("Invalid PeriodicQuery. Query must possess a PeriodicQuery Filter.");
}
} catch (Exception e) {
throw new PeriodicQueryCreationException(e);
}
}
use of org.apache.rya.periodic.notification.notification.PeriodicNotification in project incubator-rya by apache.
the class PeriodicNotificationProvider method getCommandNotifications.
private Collection<CommandNotification> getCommandNotifications(Snapshot sx, Collection<PeriodicQueryMetadata> metadata) {
Set<CommandNotification> notifications = new HashSet<>();
int i = 1;
for (PeriodicQueryMetadata meta : metadata) {
// offset initial wait to avoid overloading system
PeriodicNotification periodic = new PeriodicNotification(getQueryId(meta.getNodeId(), sx), meta.getPeriod(), TimeUnit.MILLISECONDS, i * 5000);
notifications.add(new CommandNotification(Command.ADD, periodic));
i++;
}
return notifications;
}
use of org.apache.rya.periodic.notification.notification.PeriodicNotification in project incubator-rya by apache.
the class PeriodicNotificationCoordinatorExecutor method addNotification.
private void addNotification(Notification notification) {
Preconditions.checkArgument(notification instanceof PeriodicNotification);
PeriodicNotification notify = (PeriodicNotification) notification;
if (!serviceMap.containsKey(notification.getId())) {
ScheduledFuture<?> future = producerThreadPool.scheduleAtFixedRate(new NotificationProducer(notify), notify.getInitialDelay(), notify.getPeriod(), notify.getTimeUnit());
serviceMap.put(notify.getId(), future);
}
}
Aggregations