Skip to content

Instantly share code, notes, and snippets.

View JamieAP's full-sized avatar
🎯
Focusing

Jamie JamieAP

🎯
Focusing
View GitHub Profile
@JamieAP
JamieAP / express-example.js
Last active December 26, 2015 17:29
Express Example
app.get('/', function (req, res) {
res.send('Hello World!');
});
@JamieAP
JamieAP / HelloWorld.java
Created December 26, 2015 17:31
Spark Example
import static spark.Spark.*;
public class HelloWorld {
public static void main(String[] args) {
get("/hello", (req, res) -> "Hello World");
}
}
NioEventLoopGroup acceptorGroup = new NioEventLoopGroup(2); // 2 threads
NioEventLoopGroup handlerGroup = new NioEventLoopGroup(10); // 10 threads
NioEventLoopGroup acceptorGroup = new NioEventLoopGroup(2); // 2 threads
NioEventLoopGroup handlerGroup = new NioEventLoopGroup(10); // 10 threads
try {
ServerBootstrap b = new ServerBootstrap();
b.group(acceptorGroup, handlerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new MySocketInitialiser())
.option(ChannelOption.SO_BACKLOG, 5)
.childOption(ChannelOption.SO_KEEPALIVE, true);
b.localAddress(port).bind().sync();
LOG.info("Started on port {}", port);
/**
* Performs the initial set up of sockets as they connect to Netty.
* Registers the pipeline of handlers that received messages are passed through
*/
public class MySocketInitialiser extends ChannelInitializer<SocketChannel> {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
public class JsonDecoder<T> extends MessageToMessageDecoder<String, T> {
private static final Gson GSON = new GsonBuilder().create();
private final Class<T> clazz;
public JsonDecoder(Class<T> clazz, Class<?>... acceptedMsgTypes) {
super(acceptedMsgTypes);
this.clazz = checkNotNull(clazz);
}
INFO [2016-02-16 12:06:38,880] nettytest.SocketServer: Started on port 9000
# jamie at eduD692.kent.ac.uk in ~ [12:08:52]
$ telnet localhost 9000
Trying ::1...
Connected to localhost.
Escape character is '^]'.
{"firstName":"Jamie", "lastName":"Perkins"}
Your name is Jamie Perkins!