本文整理汇总了Java中io.netty.util.ReferenceCounted类的典型用法代码示例。如果您正苦于以下问题:Java ReferenceCounted类的具体用法?Java ReferenceCounted怎么用?Java ReferenceCounted使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ReferenceCounted类属于io.netty.util包,在下文中一共展示了ReferenceCounted类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tearDown
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@After
public void tearDown() {
for (Object obj : objectsToRelease) {
if (!(obj instanceof ReferenceCounted)) {
continue;
}
ReferenceCounted referenceCountedObject = (ReferenceCounted) obj;
assertThat(referenceCountedObject.refCnt())
.withFailMessage("Trying to free %s but it has a ref count of %d", obj, referenceCountedObject.refCnt())
.isEqualTo(1);
referenceCountedObject.release();
}
}
示例2: testFreeCalled
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Test
public void testFreeCalled() throws Exception {
final CountDownLatch free = new CountDownLatch(1);
final ReferenceCounted holder = new AbstractReferenceCounted() {
@Override
protected void deallocate() {
free.countDown();
}
};
StringInboundHandler handler = new StringInboundHandler();
setUp(handler);
peer.writeAndFlush(holder).sync();
assertTrue(free.await(10, TimeUnit.SECONDS));
assertTrue(handler.called);
}
示例3: shouldHandleTwoMessagesInOneBatch
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
/**
* This test makes sure that even when more requests arrive in the same batch, they
* get emitted as separate messages.
*/
@Test
public void shouldHandleTwoMessagesInOneBatch() {
channel.writeInbound(Unpooled.buffer().writeBytes(GET_REQUEST).writeBytes(GET_REQUEST));
BinaryMemcacheRequest request = channel.readInbound();
assertThat(request, instanceOf(BinaryMemcacheRequest.class));
assertThat(request, notNullValue());
request.release();
Object lastContent = channel.readInbound();
assertThat(lastContent, instanceOf(LastMemcacheContent.class));
((ReferenceCounted) lastContent).release();
request = channel.readInbound();
assertThat(request, instanceOf(BinaryMemcacheRequest.class));
assertThat(request, notNullValue());
request.release();
lastContent = channel.readInbound();
assertThat(lastContent, instanceOf(LastMemcacheContent.class));
((ReferenceCounted) lastContent).release();
}
示例4: startAsync
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Override
public void startAsync(final Executor executor, final Runnable runnable) {
Channel channel = ctx.channel();
channel.attr(NEED_FLUSH).set(false);
channel.attr(ASYNC).set(true);
ReferenceCounted body = ((ByteBufHolder) req).content();
body.retain();
executor.execute(() -> {
try {
runnable.run();
} finally {
body.release();
}
});
}
示例5: releaseContentChunks
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
public void releaseContentChunks() {
if (!contentChunksWillBeReleasedExternally) {
contentChunks.forEach(ReferenceCounted::release);
}
// Now that the chunks have been released we should clear the chunk list - we can no longer rely on the chunks
// for anything, and if this method is called a second time we don't want to re-release the chunks
// (which would screw up the reference counting).
contentChunks.clear();
}
示例6: touch
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Override
public ReferenceCounted touch(Object hint) {
for (EncapsulatedRakNetPacket packet : packets) {
packet.touch(hint);
}
return this;
}
示例7: write
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
/**
* This method is called by users of the ProxyConnection to send stuff out
* over the socket.
*
* @param msg
*/
void write(Object msg) {
if (msg instanceof ReferenceCounted) {
LOG.debug("Retaining reference counted message");
((ReferenceCounted) msg).retain();
}
doWrite(msg);
}
示例8: write
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Override
void write(Object msg) {
LOG.debug("Requested write of {}", msg);
if (msg instanceof ReferenceCounted) {
LOG.debug("Retaining reference counted message");
((ReferenceCounted) msg).retain();
}
if (is(DISCONNECTED) && msg instanceof HttpRequest) {
LOG.debug("Currently disconnected, connect and then write the message");
connectAndWrite((HttpRequest) msg);
} else {
if (isConnecting()) {
synchronized (connectLock) {
if (isConnecting()) {
LOG.debug("Attempted to write while still in the process of connecting, waiting for connection.");
clientConnection.stopReading();
try {
connectLock.wait(30000);
} catch (InterruptedException ie) {
LOG.warn("Interrupted while waiting for connect monitor");
}
}
}
}
// only write this message if a connection was established and is not in the process of disconnecting or
// already disconnected
if (isConnecting() || getCurrentState().isDisconnectingOrDisconnected()) {
LOG.debug("Connection failed or timed out while waiting to write message to server. Message will be discarded: {}", msg);
return;
}
LOG.debug("Using existing connection to: {}", remoteAddress);
doWrite(msg);
}
}
示例9: writeToChannel
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
/**
* This is the method that executing writing to channel.
* It will be used both write0 and {@link com.linkedin.mitm.proxy.connectionflow.steps.ConnectionFlowStep}
*
* @param channel which channel to write to
* @param object which object to write to.
*
* */
private ChannelFuture writeToChannel(final Channel channel, final Object object) {
if (channel == null) {
throw new IllegalStateException("Failed to write to channel because channel is null");
}
if (object instanceof ReferenceCounted) {
LOG.debug("Retaining reference counted message");
((ReferenceCounted) object).retain();
}
if (LOG.isDebugEnabled()) {
LOG.debug(String.format("Writing in channel [%s]: %s", channel.toString(), object));
}
return channel.writeAndFlush(object);
}
示例10: write
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Override
void write(Object msg) {
LOG.debug("Requested write of {}", msg);
if (msg instanceof ReferenceCounted) {
LOG.debug("Retaining reference counted message");
((ReferenceCounted) msg).retain();
}
if (is(DISCONNECTED) && msg instanceof HttpRequest) {
LOG.debug("Currently disconnected, connect and then write the message");
connectAndWrite((HttpRequest) msg);
} else {
synchronized (connectLock) {
if (isConnecting()) {
LOG.debug("Attempted to write while still in the process of connecting, waiting for connection.");
clientConnection.stopReading();
try {
connectLock.wait(30000);
} catch (InterruptedException ie) {
LOG.warn("Interrupted while waiting for connect monitor");
}
if (is(DISCONNECTED)) {
LOG.debug("Connection failed while we were waiting for it, don't write");
return;
}
}
}
LOG.debug("Using existing connection to: {}", remoteAddress);
doWrite(msg);
}
}
示例11: acceptOutboundMessage
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Override
public boolean acceptOutboundMessage(Object msg) throws Exception {
final Message message = (Message) msg;
final Protocol protocol = this.codecContext.getSession().getProtocol();
final MessageRegistration registration = protocol.outbound().findByMessageType(message.getClass()).orElse(null);
if (registration == null) {
throw new EncoderException("Message type (" + message.getClass().getName() +
") is not registered in state " + this.codecContext.getSession().getProtocolState().name() + "!");
}
final List<Processor> processors = ((MessageRegistration) protocol.outbound()
.findByMessageType(message.getClass()).get()).getProcessors();
// Only process if there are processors found
if (!processors.isEmpty()) {
final List<Object> messages = new ArrayList<>();
for (Processor processor : processors) {
// The processor should handle the output messages
processor.process(this.codecContext, message, messages);
}
if (message instanceof ReferenceCounted && !messages.contains(message)) {
((ReferenceCounted) message).release();
}
if (!messages.isEmpty()) {
this.messages.set(messages);
}
return true;
}
return false;
}
示例12: release
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
protected void release()
{
uploadedFiles.forEach(ReferenceCounted::release);
if(this.buffer != null)
{
this.buffer.release();
}
}
示例13: refCnt
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Override
public int refCnt() {
if (message instanceof ReferenceCounted) {
return ((ReferenceCounted) message).refCnt();
} else {
return 1;
}
}
示例14: rejectReferenceCounted
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Test
public void rejectReferenceCounted() {
AbstractReferenceCounted item = new AbstractReferenceCounted() {
@Override
protected void deallocate() {}
@Override
public ReferenceCounted touch(Object hint) {
return this;
}
};
StreamMessageAndWriter<Object> stream = newStreamWriter(ImmutableList.of(item));
assertThatThrownBy(() -> stream.write(item)).isInstanceOf(IllegalArgumentException.class);
}
示例15: channelRead
import io.netty.util.ReferenceCounted; //导入依赖的package包/类
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof Message) {
Message message = (Message) msg;
if (currentPayload == null) {
log.error("No payload received for request id '{}': {}", message.getId(), message);
ctx.fireExceptionCaught(
new RuntimeException("No payload received for request id '" + message.getId() + "'"));
reset();
return;
}
if (error) {
log.error("Multiple payloads received for request id '{}': {}", message.getId(), message);
ctx.fireExceptionCaught(
new RuntimeException(
"Multiple payloads received for request id '" + message.getId() + "'"));
reset();
return;
}
ServerRequest request =
new ServerRequest(message.getId(), message.expectsResponse(), currentPayload);
ctx.fireChannelRead(request);
reset();
} else {
if (currentPayload != null) {
error = true;
return;
}
if (msg instanceof ReferenceCounted) {
currentPayload = ((ReferenceCounted) msg).retain();
} else {
currentPayload = msg;
}
}
}