/** * Shortcut for {@link #writeBufferedP(DistributedFunction, * DistributedBiConsumer, DistributedConsumer, DistributedConsumer)} with * a no-op {@code destroyFn}. */ @Nonnull public static <W, T> DistributedSupplier<Processor> writeBufferedP( @Nonnull DistributedFunction<? super Context, ? extends W> createFn, @Nonnull DistributedBiConsumer<? super W, ? super T> onReceiveFn, @Nonnull DistributedConsumer<? super W> flushFn ) { return writeBufferedP(createFn, onReceiveFn, flushFn, DistributedConsumer.noop()); }
/** * Returns a supplier of processors for * {@link Sinks#socket(String, int)}. */ public static <T> ProcessorMetaSupplier writeSocketP( @Nonnull String host, int port, @Nonnull DistributedFunction<? super T, ? extends String> toStringFn, @Nonnull Charset charset ) { checkSerializable(toStringFn, "toStringFn"); String charsetName = charset.name(); return preferLocalParallelismOne(writeBufferedP( index -> new BufferedWriter(new OutputStreamWriter(new Socket(host, port).getOutputStream(), charsetName)), (bufferedWriter, item) -> { @SuppressWarnings("unchecked") T t = (T) item; bufferedWriter.write(toStringFn.apply(t)); bufferedWriter.write('\n'); }, BufferedWriter::flush, BufferedWriter::close )); }
/** * Use {@link SinkProcessors#writeFileP(String, DistributedFunction, Charset, boolean)} */ public static <T> ProcessorMetaSupplier metaSupplier( @Nonnull String directoryName, @Nonnull DistributedFunction<? super T, ? extends String> toStringFn, @Nonnull String charset, boolean append) { return ProcessorMetaSupplier.preferLocalParallelismOne(writeBufferedP( ctx -> createBufferedWriter(Paths.get(directoryName), ctx.globalProcessorIndex(), charset, append), (fileWriter, item) -> { fileWriter.write(toStringFn.apply((T) item)); fileWriter.newLine(); }, BufferedWriter::flush, BufferedWriter::close )); }
/** * Creates and returns the {@link Sink} with the components you supplied to * this builder. */ @Nonnull public Sink<T> build() { Preconditions.checkNotNull(receiveFn, "receiveFn must be set"); DistributedSupplier<Processor> supplier = writeBufferedP(createFn, receiveFn, flushFn, destroyFn); return new SinkImpl<>(name, ProcessorMetaSupplier.of(supplier, preferredLocalParallelism)); } }
@Nonnull @Override public Collection<? extends Processor> get(int count) { DistributedFunction<Processor.Context, JmsContext> createFn = jet -> { Session session = sessionF.apply(connection); Destination destination = isTopic ? session.createTopic(name) : session.createQueue(name); MessageProducer producer = session.createProducer(destination); return new JmsContext(session, producer); }; DistributedBiConsumer<JmsContext, T> onReceiveFn = (jmsContext, item) -> { Message message = messageFn.apply(jmsContext.session, item); sendFn.accept(jmsContext.producer, message); }; DistributedConsumer<JmsContext> flushF = jmsContext -> flushFn.accept(jmsContext.session); DistributedConsumer<JmsContext> destroyFn = jmsContext -> { jmsContext.producer.close(); jmsContext.session.close(); }; DistributedSupplier<Processor> supplier = writeBufferedP(createFn, onReceiveFn, flushF, destroyFn); return Stream.generate(supplier).limit(count).collect(toList()); }