Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.0.0.CR2</version>
<version>4.0.12.Final</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpContent;
import io.netty.handler.codec.http.HttpHeaders;
Expand All @@ -18,7 +18,7 @@
import java.util.Map;
import java.util.Map.Entry;

public class NettyHttpServerHandlerAdapter extends ChannelInboundMessageHandlerAdapter<Object> {
public class NettyHttpServerHandlerAdapter extends ChannelInboundHandlerAdapter {

private HttpRequest request;

Expand All @@ -34,7 +34,8 @@ public NettyHttpServerHandlerAdapter(NettyHttpServerHandler handler) {
}

@Override
public void endMessageReceived(ChannelHandlerContext ctx) throws Exception {
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
super.channelReadComplete(ctx);
ctx.flush();
}

Expand All @@ -45,7 +46,7 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
}

@Override
public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Exception {
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof HttpRequest) {
request = (HttpRequest) msg;

Expand Down Expand Up @@ -87,7 +88,7 @@ public void messageReceived(ChannelHandlerContext ctx, Object msg) throws Except

private static void send100Continue(ChannelHandlerContext ctx, HttpRequest request) {
HttpResponse response = new DefaultHttpResponse(request.getProtocolVersion(), HttpResponseStatus.CONTINUE);
ctx.nextOutboundMessageBuffer().add(response);
ctx.write(response);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.n3r.shorturl.netty;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
Expand All @@ -18,9 +17,8 @@
public class ShortUrlNettyHandler implements NettyHttpServerHandler {
public static final RedisClient redis = new RedisClient("132.35.81.197:6379");

@Override
public void handle(ChannelHandlerContext ctx, HttpRequest request, String urlPattern, Map<String, String> parameters) {
String rspCnt = "/favicon.ico".equalsIgnoreCase(urlPattern) ? "" : "/generate".equalsIgnoreCase(urlPattern) ?
String rspCnt = "/favicon.ico".equalsIgnoreCase(urlPattern) ? "" : "/generate".equalsIgnoreCase(urlPattern) ?
generate(ctx, request, parameters) : query(ctx, request, parameters);

FullHttpResponse response = createResponse(request, rspCnt);
Expand All @@ -31,10 +29,10 @@ public void handle(ChannelHandlerContext ctx, HttpRequest request, String urlPat
response.headers().set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.KEEP_ALIVE);
}

ctx.nextOutboundMessageBuffer().add(response);
ctx.write(response);

if (!keepAlive) {
ctx.flush().addListener(ChannelFutureListener.CLOSE);
ctx.flush().close();
}
}

Expand Down
181 changes: 90 additions & 91 deletions src/main/java/org/n3r/shorturl/redis/RedisClient.java
Original file line number Diff line number Diff line change
@@ -1,91 +1,90 @@
package org.n3r.shorturl.redis;

import java.io.Closeable;
import java.io.IOException;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisClient implements Closeable {

public JedisPool pool;

/**
* 生成多线程共享的使用的jedis对象。
* @param redisConnect 诸如127.0.0.1:6379的HOST:PORT连接字符串
*/
public RedisClient(String redisConnect) {
HostPort hostPort = new HostPort(redisConnect, "127.0.0.1", 6379);
pool = new JedisPool(new JedisPoolConfig(), hostPort.getHost(), hostPort.getPort());
}

public RedisClient(String host, int port) {
pool = new JedisPool(new JedisPoolConfig(), host, port);
}



public String get(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.get(key);
} finally {
pool.returnResource(jedis);
}
}

public String set(String key, String value) {
Jedis jedis = pool.getResource();
try {
return jedis.set(key, value);
} finally {
pool.returnResource(jedis);
}
}

public String setex(String key, int seconds, String value) {
Jedis jedis = pool.getResource();
try {
return jedis.setex(key, seconds, value);
} finally {
pool.returnResource(jedis);
}
}

public Long incr(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.incr(key);
} finally {
pool.returnResource(jedis);
}
}

public void expire(String key, int seconds) {
Jedis jedis = pool.getResource();
try {
jedis.expire(key, seconds);
}
finally {
pool.returnResource(jedis);
}
}

public Set<String> keys(String pattern) {
Jedis jedis = pool.getResource();
try {
return jedis.keys(pattern);
}
finally {
pool.returnResource(jedis);
}
}

@Override
public void close() throws IOException {
pool.destroy();
}

}
package org.n3r.shorturl.redis;

import java.io.Closeable;
import java.io.IOException;
import java.util.Set;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

public class RedisClient implements Closeable {

public JedisPool pool;

/**
* 生成多线程共享的使用的jedis对象。
* @param redisConnect 诸如127.0.0.1:6379的HOST:PORT连接字符串
*/
public RedisClient(String redisConnect) {
HostPort hostPort = new HostPort(redisConnect, "127.0.0.1", 6379);
pool = new JedisPool(new JedisPoolConfig(), hostPort.getHost(), hostPort.getPort());
}

public RedisClient(String host, int port) {
pool = new JedisPool(new JedisPoolConfig(), host, port);
}



public String get(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.get(key);
} finally {
pool.returnResource(jedis);
}
}

public String set(String key, String value) {
Jedis jedis = pool.getResource();
try {
return jedis.set(key, value);
} finally {
pool.returnResource(jedis);
}
}

public String setex(String key, int seconds, String value) {
Jedis jedis = pool.getResource();
try {
return jedis.setex(key, seconds, value);
} finally {
pool.returnResource(jedis);
}
}

public Long incr(String key) {
Jedis jedis = pool.getResource();
try {
return jedis.incr(key);
} finally {
pool.returnResource(jedis);
}
}

public void expire(String key, int seconds) {
Jedis jedis = pool.getResource();
try {
jedis.expire(key, seconds);
}
finally {
pool.returnResource(jedis);
}
}

public Set<String> keys(String pattern) {
Jedis jedis = pool.getResource();
try {
return jedis.keys(pattern);
}
finally {
pool.returnResource(jedis);
}
}

public void close() throws IOException {
pool.destroy();
}

}