Skip to content

Instantly share code, notes, and snippets.

View jbrisbin's full-sized avatar

Jon Brisbin jbrisbin

View GitHub Profile
@jbrisbin
jbrisbin / amqpstreams.groovy
Created October 7, 2014 17:44
Teaser of AmqpStreams
def stream1 = AmqpStreams.fromQueue(Queue.lookup('test')
.durable(true)
.bind('test'))
def stream2 = AmqpStreams.fromQueue('test')
Streams.merge(stream1, stream2)
.dispatchOn(environment)
@jbrisbin
jbrisbin / StreamRenderer.java
Created November 6, 2014 08:55
Ratpack Renderer for Reactor Streams
@Bean
public Renderer<Stream> personRenderer() {
return new RendererSupport<Stream>() {
@Override
public void render(Context ctx, Stream s) throws Exception {
ctx.promise(f -> s.consume(f::success))
.then(o -> ctx.render(json(o)));
}
};
}
@Bean
public Action<Chain> handlers(PersonRepository persons,
HotStream<Person> personStream,
ObjectMapper jsonMapper,
ModelMapper beanMapper) {
return (chain) -> {
chain.handler("person", ctx ->
ctx.byMethod(spec ->
spec
.get(c -> {
@jbrisbin
jbrisbin / ReactiveStreamApi.java
Created December 31, 2014 15:05
Sketch of ReactiveStream interface-based API
public interface PersonStream extends ReactiveStream<Person> {
@MapAction("@personRepo.merge($1, $2)")
ReactivePromise<Person> merge(String id, Person p);
}
@ReactiveRestController
public class MyController {
@jbrisbin
jbrisbin / BroadcastStream.java
Last active October 2, 2015 21:10
Simple Broadcast Stream example using #ProjectReactor
static {
// Only done once, statically, and shared across this classloader
Environment.initialize();
}
// Create a Stream subclass we can sink values into
Broadcaster<String> b = Broadcaster.create();
b
// dispatch onto a Thread other than 'main'
@jbrisbin
jbrisbin / pom.xml
Last active August 29, 2015 14:13
Example of using Reactor in a Maven project
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>my-project</artifactId>
<version>1.0.0.BUILD-SNAPSHOT</version>
@jbrisbin
jbrisbin / ReactorDispatchingExample.java
Created February 19, 2015 21:55
Demo class for calculating Pi using Reactor
import org.apache.commons.lang.time.StopWatch;
import reactor.Environment;
import reactor.fn.BiFunction;
import reactor.rx.Streams;
import java.util.concurrent.TimeUnit;
/**
* Demo class for calculating Pi using Reactor.
*/
@jbrisbin
jbrisbin / ReactiveStreamsIO.java
Last active August 29, 2015 14:16
Sketch of Reactive Streams interfaces for Client/Server interaction
interface Channel<IN, OUT> extends Publisher<IN> {
Publisher<Boolean> write(Publisher<? extends OUT> data);
}
interface ClientSocketOptions {
SocketAddress connectAddress();
@jbrisbin
jbrisbin / TcpConnection.java
Last active August 29, 2015 14:17
New RIPC abstractions
public interface TcpConnection<R, W> {
Reader<R> reader();
Writer<W> writer();
public interface Reader<R> extends Publisher<R> {
<NEWR> Reader<NEWR> intercept(Interceptor<R, NEWR> interceptor);
}
@jbrisbin
jbrisbin / ExampleReactorCode.java
Last active August 29, 2015 14:17
Example RIPC server wiring using Reactor
ReactorTcpServer.listen(3000, ByteBuf.class)
.log("connection")
.consume(conn -> conn.out(conn.in().log("in"))
.log("confirmation")
.consume(buf -> LOG.info("write confirmed: {}", buf)));