概述
Jetty 是和 Eclipse 基金会相关的,但 JBoss 社区也出现了一个某些功能与其重合的项目。
Netty(http://netty.io/)被用来创建优化网络应用(服务器和客户端协议)。
优化网络应用在扩展性上比一般应用要好,
Twitter(https://blog.twitter.com/2011/twitter-search-now-3xfaster) 采用 Netty 去解决它们的性能问题就表明了 Netty 的成熟,和其在扩展性上的能力。
使用 Netty 的例子和前面展示的使用 Jetty 的例子类似,它们都遵循类似的模式:一个包含main 方法的类初始化并启动服务器,一个处理器类负责处理进来的请求
代码如下:
package com.saternos.embedded;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
public class JsonHandler extends ChannelInboundMessageHandlerAdapter<HttpRequest> {
public void messageReceived(ChannelHandlerContext channelHandlerContext, HttpRequest httpRequest) throws Exception {
StringBuffer buf = new StringBuffer();
buf.append("{"testResponse":"Hello World"}");
SimpleDateFormat dateFormatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
dateFormatter.setTimeZone(TimeZone.getTimeZone("GMT"));
Calendar time = new GregorianCalendar();
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "application/json;charset=utf-8");
response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, buf.length());
response.setHeader(HttpHeaders.Names.DATE, dateFormatter.format(time.getTime()));
response.setHeader(HttpHeaders.Names.SERVER,
TestNettyHttpServer.class.getName() + ":io.netty:netty:4.0.0.Alpha8");
response.setContent(Unpooled.copiedBuffer(buf, CharsetUtil.UTF_8));
channelHandlerContext.write(response).addListener(ChannelFutureListener.CLOSE);
}
}
package com.saternos.embedded;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpChunkAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
public class JsonServerInitializer extends ChannelInitializer<SocketChannel> {
public void initChannel(SocketChannel socketChannel) throws Exception {
ChannelPipeline pipeline = socketChannel.pipeline();
pipeline.addLast("decoder", new HttpRequestDecoder());
pipeline.addLast("aggregator", new HttpChunkAggregator(65536));
pipeline.addLast("encoder", new HttpResponseEncoder());
pipeline.addLast("chunkedWriter", new ChunkedWriteHandler());
pipeline.addLast("handler", new JsonHandler());
}
}
package com.saternos.embedded;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.socket.nio.*;
public class TestNettyHttpServer {
public static void main(String[] args) throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup())
.channel(NioServerSocketChannel.class)
.localAddress(Integer.parseInt("8000"))
.childHandler(new JsonServerInitializer());
System.out.println("Starting server on port: " + 8000);
bootstrap.bind().sync().channel().closeFuture().sync();
}
}
POM文件:
<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>org.myself</groupId>
<artifactId>temp-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>temp-demo</name>
<url>http://maven.apache.org</url>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.netty/netty -->
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty</artifactId>
<version>4.0.0.Alpha8</version>
</dependency>
</dependencies>
</project>
最后
以上就是单纯薯片为你收集整理的Netty嵌入式服务器的全部内容,希望文章能够帮你解决Netty嵌入式服务器所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复