/** * Send a message with an ID and content. * * @param id The ID associated with the message. * @param content The content of the message. * @throws PubSubException if the messaging system throws an error. */ default void send(String id, String content) throws PubSubException { send(new PubSubMessage(id, content)); }
/** * Check if the message has a {@link Signal}. * * @return true if message has a signal. */ public boolean hasSignal() { return hasMetadata() && metadata.hasSignal(); }
/** * Instantiate a PubSub using parameters from {@link BulletConfig}. * * @param config A non-null {@link BulletConfig} containing all required PubSub parameters. * @throws PubSubException if the context name is not present or cannot be parsed. */ public PubSub(BulletConfig config) throws PubSubException { this.config = config; this.context = Context.valueOf(getRequiredConfig(String.class, BulletConfig.PUBSUB_CONTEXT_NAME)); }
@Override public void send(PubSubMessage message) { // Put responseURL in the metadata so the ResponsePublisher knows to which host to send the response Metadata metadata = message.getMetadata(); metadata = metadata == null ? new Metadata() : metadata; metadata.setContent(resultURL); message.setMetadata(metadata); sendToURL(queryURL, message); } }
private Metadata withSignal(Metadata metadata, Metadata.Signal signal) { // Don't change the non-readonly bits of metadata in place since that might affect tuples emitted but pending. Metadata copy = new Metadata(signal, null); if (metadata != null) { copy.setContent(metadata.getContent()); } return copy; }
@Override public void send(PubSubMessage message) { String url = (String) message.getMetadata().getContent(); log.debug("Extracted url to which to send results: {}", url); sendToURL(url, message); } }
public static String makeMessage(String id, String content, int sequence) { return new PubSubMessage(id, content, sequence).asJSON(); } public static Object makeMessageID(String id, int index) {
/** * Constructor for a message having content, a {@link Signal} and a sequence number. * * @param id The ID associated with the message. * @param content The content of the message. * @param signal The Signal to be sent with the message. * @param sequence The sequence number of the message. */ public PubSubMessage(String id, String content, Signal signal, int sequence) { this(id, content, new Metadata(signal, null), sequence); }
/** * Method to create a PubSubException when a required argument could not be read. * * @param name The name of the argument that could not be read. * @param cause The optional {@link Throwable} that caused the exception. * @return A PubSubException indicating failure to read a required argument. */ public static PubSubException forArgument(String name, Throwable cause) { String message = "Could not read required argument: " + name; return cause == null ? new PubSubException(message) : new PubSubException(message, cause); } }
@Override public String toString() { return asJSON(); }
/** * Convenience method to fail a message that doesn't contain a sequence number. * * @param id The ID of the message to be marked as a processing failure. */ default void fail(String id) { fail(id, -1); } }
/** * Convenience method to commit a message that doesn't contain a sequence number. * * @param id The ID of the message to be marked as committed. */ default void commit(String id) { commit(id, -1); }
/** * Check if Metadata has the given signal. * * @param signal The signal to check against. * @return true if message has {@link Metadata#signal} */ public boolean hasSignal(Signal signal) { return hasSignal() && this.signal == signal; } }
@Override public void cleanup() { try { publisher.close(); } catch (Exception e) { log.error("Could not close Publisher.", e); } }
private Metadata withSignal(Metadata metadata, Metadata.Signal signal) { // Don't change the non-readonly bits of metadata in place since that might affect tuples emitted but pending. Metadata copy = new Metadata(signal, null); if (metadata != null) { copy.setContent(metadata.getContent()); } return copy; }
/** * Check if message has a given {@link Signal}. * * @param signal The signal to check for. * @return true if message has the given signal. */ public boolean hasSignal(Signal signal) { return hasMetadata() && metadata.hasSignal(signal); }
@Override public void fail(Object id) { subscriber.fail((String) id); }
@Override public void ack(Object id) { subscriber.commit((String) id); }
@Override public void fail(Object id) { subscriber.fail((String) id); }
@Override public void ack(Object id) { subscriber.commit((String) id); }