Dieses Beispiel zeigt, wie leicht sich das JBoss Netty Framework für einen eigenen Server einsetzen lässt. Wenn man das Beispiel startet und in einem Internetbrowser die Adresse http://localhost:8080/
eingibt, dann wird im Browser das Wort „OKAY“ angezeigt (nach HTTP/1.1-Standard) und serverseitig wird der Request des Clients (Browsers) auf der Konsole ausgegeben.
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
| <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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>mavenproject1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project> |
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>mavenproject1</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>mavenproject1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.jboss.netty</groupId>
<artifactId>netty</artifactId>
<version>3.2.5.Final</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
NettyServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
| package de.bennyn.examples.netty;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class NettyServer
{
public static void main(String[] args) throws Exception
{
// ChannelFactory processes all I/O requests and performs I/O to generate ChannelEvents
// ChannelFactory needs given threads
ChannelFactory factory =
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
// ServerBootstrap sets up a server (can be done manually with Channels)
ServerBootstrap bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory()
{
@Override
public ChannelPipeline getPipeline()
{
// Pipeline hanlder
return Channels.pipeline(new ConnectionHandler());
}
});
// Set TCP/IP socket options
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind server to a port
bootstrap.bind(new InetSocketAddress("localhost", 8080));
}
} |
package de.bennyn.examples.netty;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.ChannelFactory;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.ChannelPipelineFactory;
import org.jboss.netty.channel.Channels;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
public class NettyServer
{
public static void main(String[] args) throws Exception
{
// ChannelFactory processes all I/O requests and performs I/O to generate ChannelEvents
// ChannelFactory needs given threads
ChannelFactory factory =
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool());
// ServerBootstrap sets up a server (can be done manually with Channels)
ServerBootstrap bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new ChannelPipelineFactory()
{
@Override
public ChannelPipeline getPipeline()
{
// Pipeline hanlder
return Channels.pipeline(new ConnectionHandler());
}
});
// Set TCP/IP socket options
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
// Bind server to a port
bootstrap.bind(new InetSocketAddress("localhost", 8080));
}
}
ConnectionHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
| package de.bennyn.examples.netty;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
public class ConnectionHandler extends SimpleChannelHandler
{
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
{
// Print received message
ChannelBuffer buf = (ChannelBuffer) e.getMessage();
while (buf.readable())
{
System.out.print((char) buf.readByte());
System.out.flush();
}
// Send a message back
String http200 = "HTTP/1.1 200 OK" + "\r\n"
+ "Content-Type: text/html" + "\r\n"
+ "Content-Length: 4" + "\r\n"
+ "\r\n"
+ "OKAY";
Channel ch = e.getChannel();
ChannelBuffer output = ChannelBuffers.copiedBuffer(http200.getBytes());
ChannelFuture future = ch.write(output);
if (future.isDone())
{
ch.close();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
{
System.out.println("ERROR: " + e.getCause().getLocalizedMessage());
Channel ch = e.getChannel();
ch.close();
}
} |
package de.bennyn.examples.netty;
import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelFuture;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
public class ConnectionHandler extends SimpleChannelHandler
{
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
{
// Print received message
ChannelBuffer buf = (ChannelBuffer) e.getMessage();
while (buf.readable())
{
System.out.print((char) buf.readByte());
System.out.flush();
}
// Send a message back
String http200 = "HTTP/1.1 200 OK" + "\r\n"
+ "Content-Type: text/html" + "\r\n"
+ "Content-Length: 4" + "\r\n"
+ "\r\n"
+ "OKAY";
Channel ch = e.getChannel();
ChannelBuffer output = ChannelBuffers.copiedBuffer(http200.getBytes());
ChannelFuture future = ch.write(output);
if (future.isDone())
{
ch.close();
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
{
System.out.println("ERROR: " + e.getCause().getLocalizedMessage());
Channel ch = e.getChannel();
ch.close();
}
}