This commit is contained in:
地平线
2015-11-06 13:50:56 +08:00
parent fac57035ff
commit 3df5085134
6 changed files with 19 additions and 20 deletions

View File

@@ -44,7 +44,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
} }
public BsonWriter pollBsonWriter() { public BsonWriter pollBsonWriter() {
return writerPool.poll().setTiny(tiny); return writerPool.get().setTiny(tiny);
} }
public void offerBsonWriter(BsonWriter out) { public void offerBsonWriter(BsonWriter out) {
@@ -52,7 +52,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
} }
public BsonReader pollBsonReader() { public BsonReader pollBsonReader() {
return readerPool.poll(); return readerPool.get();
} }
public void offerBsonReader(BsonReader in) { public void offerBsonReader(BsonReader in) {
@@ -66,7 +66,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public <T> T convertFrom(final Type type, final byte[] bytes, int start, int len) { public <T> T convertFrom(final Type type, final byte[] bytes, int start, int len) {
if (type == null) return null; if (type == null) return null;
final BsonReader in = readerPool.poll(); final BsonReader in = readerPool.get();
in.setBytes(bytes, start, len); in.setBytes(bytes, start, len);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
T rs = (T) factory.loadDecoder(type).convertFrom(in); T rs = (T) factory.loadDecoder(type).convertFrom(in);
@@ -83,7 +83,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public byte[] convertTo(final Type type, Object value) { public byte[] convertTo(final Type type, Object value) {
if (type == null) return null; if (type == null) return null;
final BsonWriter out = writerPool.poll().setTiny(tiny); final BsonWriter out = writerPool.get().setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value); factory.loadEncoder(type).convertTo(out, value);
byte[] result = out.toArray(); byte[] result = out.toArray();
writerPool.offer(out); writerPool.offer(out);
@@ -105,7 +105,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public byte[] convertTo(Object value) { public byte[] convertTo(Object value) {
if (value == null) { if (value == null) {
final BsonWriter out = writerPool.poll().setTiny(tiny); final BsonWriter out = writerPool.get().setTiny(tiny);
out.writeNull(); out.writeNull();
byte[] result = out.toArray(); byte[] result = out.toArray();
writerPool.offer(out); writerPool.offer(out);
@@ -116,7 +116,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public ByteBuffer convertToBuffer(final Type type, Object value) { public ByteBuffer convertToBuffer(final Type type, Object value) {
if (type == null) return null; if (type == null) return null;
final BsonWriter out = writerPool.poll().setTiny(tiny); final BsonWriter out = writerPool.get().setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value); factory.loadEncoder(type).convertTo(out, value);
ByteBuffer result = out.toBuffer(); ByteBuffer result = out.toBuffer();
writerPool.offer(out); writerPool.offer(out);
@@ -125,7 +125,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public ByteBuffer convertToBuffer(Object value) { public ByteBuffer convertToBuffer(Object value) {
if (value == null) { if (value == null) {
final BsonWriter out = writerPool.poll().setTiny(tiny); final BsonWriter out = writerPool.get().setTiny(tiny);
out.writeNull(); out.writeNull();
ByteBuffer result = out.toBuffer(); ByteBuffer result = out.toBuffer();
writerPool.offer(out); writerPool.offer(out);
@@ -136,7 +136,7 @@ public final class BsonConvert extends Convert<BsonReader, BsonWriter> {
public BsonWriter convertToWriter(final Type type, Object value) { public BsonWriter convertToWriter(final Type type, Object value) {
if (type == null) return null; if (type == null) return null;
final BsonWriter out = writerPool.poll().setTiny(tiny); final BsonWriter out = writerPool.get().setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value); factory.loadEncoder(type).convertTo(out, value);
return out; return out;
} }

View File

@@ -31,7 +31,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
} }
public JsonWriter pollJsonWriter() { public JsonWriter pollJsonWriter() {
return writerPool.poll().setTiny(tiny); return writerPool.get().setTiny(tiny);
} }
public void offerJsonWriter(JsonWriter out) { public void offerJsonWriter(JsonWriter out) {
@@ -55,7 +55,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
public <T> T convertFrom(final Type type, final char[] text, int start, int len) { public <T> T convertFrom(final Type type, final char[] text, int start, int len) {
if (text == null || type == null) return null; if (text == null || type == null) return null;
final JsonReader in = readerPool.poll(); final JsonReader in = readerPool.get();
in.setText(text, start, len); in.setText(text, start, len);
T rs = (T) factory.loadDecoder(type).convertFrom(in); T rs = (T) factory.loadDecoder(type).convertFrom(in);
readerPool.offer(in); readerPool.offer(in);
@@ -65,7 +65,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
public String convertTo(final Type type, Object value) { public String convertTo(final Type type, Object value) {
if (type == null) return null; if (type == null) return null;
if (value == null) return "null"; if (value == null) return "null";
final JsonWriter out = writerPool.poll().setTiny(tiny); final JsonWriter out = writerPool.get().setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value); factory.loadEncoder(type).convertTo(out, value);
String result = out.toString(); String result = out.toString();
writerPool.offer(out); writerPool.offer(out);
@@ -102,7 +102,7 @@ public final class JsonConvert extends Convert<JsonReader, JsonWriter> {
public byte[] convertToUTF8Bytes(final Type type, Object value) { public byte[] convertToUTF8Bytes(final Type type, Object value) {
if (type == null) return null; if (type == null) return null;
if (value == null) return new byte[]{110, 117, 108, 108}; if (value == null) return new byte[]{110, 117, 108, 108};
final JsonWriter out = writerPool.poll().setTiny(tiny); final JsonWriter out = writerPool.get().setTiny(tiny);
factory.loadEncoder(type).convertTo(out, value); factory.loadEncoder(type).convertTo(out, value);
byte[] result = out.toUTF8Bytes(); byte[] result = out.toUTF8Bytes();
writerPool.offer(out); writerPool.offer(out);

View File

@@ -90,7 +90,7 @@ public class Context {
} }
public ByteBuffer pollBuffer() { public ByteBuffer pollBuffer() {
return bufferPool.poll(); return bufferPool.get();
} }
public void offerBuffer(ByteBuffer buffer) { public void offerBuffer(ByteBuffer buffer) {

View File

@@ -34,7 +34,7 @@ public final class PrepareRunner implements Runnable {
final PrepareServlet prepare = context.prepare; final PrepareServlet prepare = context.prepare;
final ObjectPool<? extends Response> responsePool = context.responsePool; final ObjectPool<? extends Response> responsePool = context.responsePool;
if (data != null) { if (data != null) {
final Response response = responsePool.poll(); final Response response = responsePool.get();
response.init(channel); response.init(channel);
try { try {
prepare.prepare(data, response.request, response); prepare.prepare(data, response.request, response);
@@ -65,7 +65,7 @@ public final class PrepareRunner implements Runnable {
// System.println(new String(bs)); // System.println(new String(bs));
// } // }
buffer.flip(); buffer.flip();
final Response response = responsePool.poll(); final Response response = responsePool.get();
response.init(channel); response.init(channel);
try { try {
prepare.prepare(buffer, response.request, response); prepare.prepare(buffer, response.request, response);

View File

@@ -112,7 +112,7 @@ public final class Transport {
} }
public ByteBuffer pollBuffer() { public ByteBuffer pollBuffer() {
return bufferPool.poll(); return bufferPool.get();
} }
public void offerBuffer(ByteBuffer buffer) { public void offerBuffer(ByteBuffer buffer) {
@@ -131,7 +131,6 @@ public final class Transport {
final boolean rand = addr == null; final boolean rand = addr == null;
try { try {
if ("TCP".equalsIgnoreCase(protocol)) { if ("TCP".equalsIgnoreCase(protocol)) {
Socket socket = null;
AsynchronousSocketChannel channel = null; AsynchronousSocketChannel channel = null;
if (rand) { //随机取地址 if (rand) { //随机取地址
int p = 0; int p = 0;
@@ -154,7 +153,6 @@ public final class Transport {
iex.printStackTrace(); iex.printStackTrace();
if (i == remoteAddres.length - 1) { if (i == remoteAddres.length - 1) {
p = 0; p = 0;
socket = null;
channel = null; channel = null;
} }
} }

View File

@@ -15,7 +15,7 @@ import java.util.logging.*;
* @author zhangjx * @author zhangjx
* @param <T> * @param <T>
*/ */
public final class ObjectPool<T> { public final class ObjectPool<T> implements Supplier<T> {
private static final Logger logger = Logger.getLogger(ObjectPool.class.getSimpleName()); private static final Logger logger = Logger.getLogger(ObjectPool.class.getSimpleName());
@@ -63,7 +63,8 @@ public final class ObjectPool<T> {
this.creator = creator; this.creator = creator;
} }
public T poll() { @Override
public T get() {
T result = queue.poll(); T result = queue.poll();
if (result == null) { if (result == null) {
if (creatCounter != null) creatCounter.incrementAndGet(); if (creatCounter != null) creatCounter.incrementAndGet();