Search in sources :

Example 1 with PeriodicNotification

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;
}
Also used : BasicNotification(org.apache.rya.periodic.notification.notification.BasicNotification) JsonPrimitive(com.google.gson.JsonPrimitive) JsonObject(com.google.gson.JsonObject) PeriodicNotification(org.apache.rya.periodic.notification.notification.PeriodicNotification) Notification(org.apache.rya.periodic.notification.api.Notification) PeriodicNotification(org.apache.rya.periodic.notification.notification.PeriodicNotification) CommandNotification(org.apache.rya.periodic.notification.notification.CommandNotification) BasicNotification(org.apache.rya.periodic.notification.notification.BasicNotification)

Example 2 with PeriodicNotification

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);
    }
}
Also used : PeriodicQueryStorageException(org.apache.rya.indexing.pcj.storage.PeriodicQueryStorageException) UnsupportedQueryException(org.apache.rya.indexing.pcj.fluo.app.query.UnsupportedQueryException) MalformedQueryException(org.openrdf.query.MalformedQueryException) PeriodicNotification(org.apache.rya.periodic.notification.notification.PeriodicNotification) PeriodicQueryNode(org.apache.rya.indexing.pcj.fluo.app.query.PeriodicQueryNode) FluoQuery(org.apache.rya.indexing.pcj.fluo.app.query.FluoQuery)

Example 3 with PeriodicNotification

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);
    }
}
Also used : PeriodicNotification(org.apache.rya.periodic.notification.notification.PeriodicNotification) PeriodicQueryNode(org.apache.rya.indexing.pcj.fluo.app.query.PeriodicQueryNode) FluoQuery(org.apache.rya.indexing.pcj.fluo.app.query.FluoQuery) UnsupportedQueryException(org.apache.rya.indexing.pcj.fluo.app.query.UnsupportedQueryException) PeriodicQueryStorageException(org.apache.rya.indexing.pcj.storage.PeriodicQueryStorageException) MalformedQueryException(org.openrdf.query.MalformedQueryException)

Example 4 with PeriodicNotification

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;
}
Also used : PeriodicQueryMetadata(org.apache.rya.indexing.pcj.fluo.app.query.PeriodicQueryMetadata) PeriodicNotification(org.apache.rya.periodic.notification.notification.PeriodicNotification) CommandNotification(org.apache.rya.periodic.notification.notification.CommandNotification) HashSet(java.util.HashSet)

Example 5 with PeriodicNotification

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);
    }
}
Also used : PeriodicNotification(org.apache.rya.periodic.notification.notification.PeriodicNotification)

Aggregations

PeriodicNotification (org.apache.rya.periodic.notification.notification.PeriodicNotification)6 CommandNotification (org.apache.rya.periodic.notification.notification.CommandNotification)3 FluoQuery (org.apache.rya.indexing.pcj.fluo.app.query.FluoQuery)2 PeriodicQueryNode (org.apache.rya.indexing.pcj.fluo.app.query.PeriodicQueryNode)2 UnsupportedQueryException (org.apache.rya.indexing.pcj.fluo.app.query.UnsupportedQueryException)2 PeriodicQueryStorageException (org.apache.rya.indexing.pcj.storage.PeriodicQueryStorageException)2 BasicNotification (org.apache.rya.periodic.notification.notification.BasicNotification)2 MalformedQueryException (org.openrdf.query.MalformedQueryException)2 JsonObject (com.google.gson.JsonObject)1 JsonPrimitive (com.google.gson.JsonPrimitive)1 HashSet (java.util.HashSet)1 PeriodicQueryMetadata (org.apache.rya.indexing.pcj.fluo.app.query.PeriodicQueryMetadata)1 Notification (org.apache.rya.periodic.notification.api.Notification)1 Test (org.junit.Test)1