This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.get('/', function (req, res) { | |
res.send('Hello World!'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static spark.Spark.*; | |
public class HelloWorld { | |
public static void main(String[] args) { | |
get("/hello", (req, res) -> "Hello World"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NioEventLoopGroup acceptorGroup = new NioEventLoopGroup(2); // 2 threads | |
NioEventLoopGroup handlerGroup = new NioEventLoopGroup(10); // 10 threads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NioEventLoopGroup acceptorGroup = new NioEventLoopGroup(2); // 2 threads | |
NioEventLoopGroup handlerGroup = new NioEventLoopGroup(10); // 10 threads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INFO [2016-02-16 12:06:38,880] nettytest.SocketServer: Started on port 9000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Your name is Jamie Perkins! |
OlderNewer