@Override public Connector createConnector(final Map<String, Object> configuration, final BufferHandler handler, final ClientConnectionLifeCycleListener listener, final Executor closeExecutor, final Executor threadPool, final ScheduledExecutorService scheduledThreadPool, final ClientProtocolManager protocolManager) { return new NettyConnector(configuration, handler, listener, closeExecutor, threadPool, scheduledThreadPool); }
@Override public Future<?> shutdownGracefully() { return shutdownGracefully(100, 3000, TimeUnit.MILLISECONDS); }
@Override public final void write(final ActiveMQBuffer buffer) { write(buffer, false, false); }
private boolean canWrite(final int requiredCapacity) { //evaluate if the write request could be taken: //there is enough space in the write buffer? //The pending writes on event loop will eventually go into the Netty write buffer, hence consider them //as part of the heuristic! final long pendingWritesOnEventLoop = this.pendingWritesOnEventLoop(); final long totalPendingWrites = pendingWritesOnEventLoop + this.pendingWritesOnChannel(); final boolean canWrite; if (requiredCapacity > this.writeBufferHighWaterMark) { canWrite = totalPendingWrites == 0; } else { canWrite = (totalPendingWrites + requiredCapacity) <= this.writeBufferHighWaterMark; } return canWrite; }
@Override public void addChannelHandlers(ChannelPipeline pipeline) { pipeline.addLast("activemq-decoder", new ActiveMQFrameDecoder2()); }
public final int pendingWritesOnChannel() { return batchBufferSize(this.channel, this.writeBufferHighWaterMark); }
private void checkConnectionState() { if (this.closed || !this.channel.isActive()) { throw new IllegalStateException("Connection " + getID() + " closed or disconnected"); } }
@Override public void finalize() throws Throwable { close(); super.finalize(); }
/** * Creates an arbitrary number of random bytes * * @param size the number of random bytes to create * @return An array of random bytes */ private static byte[] randomBytes(int size) { byte[] bytes = new byte[size]; for (int index = 0; index < size; index++) { bytes[index] = (byte) randomNumber(0, 255); } return bytes; }
public static void clearThreadPools() { SharedEventLoopGroup.forceShutdown(); }
@Override public String toString() { return "NettyConnector [host=" + host + ", port=" + port + ", httpEnabled=" + httpEnabled + ", httpUpgradeEnabled=" + httpUpgradeEnabled + ", useServlet=" + useServlet + ", servletPath=" + servletPath + ", sslEnabled=" + sslEnabled + ", useNio=" + true + getHttpUpgradeInfo() + "]"; }
public static X509Certificate[] getCertsFromConnection(RemotingConnection remotingConnection) { X509Certificate[] certificates = null; if (remotingConnection != null) { Connection transportConnection = remotingConnection.getTransportConnection(); if (transportConnection instanceof NettyConnection) { certificates = org.apache.activemq.artemis.utils.CertificateUtil.getCertsFromChannel(((NettyConnection) transportConnection).getChannel()); } } return certificates; }
@Override public void channelInactive(final ChannelHandlerContext ctx) throws Exception { if (task != null) { task.close(); } super.channelInactive(ctx); }
@Override public void connectionReadyForWrites(Object connectionID, boolean ready) { NettyConnection connection = (NettyConnection) connections.get(connectionID); if (connection != null) { connection.fireReady(ready); } listener.connectionReadyForWrites(connectionID, ready); }
public static synchronized void forceShutdown() { if (instance != null) { instance.forEach(executor -> executor.shutdownGracefully(0, 0, TimeUnit.MILLISECONDS)); instance.channelFactoryCount.set(0); instance = null; } }
@Override public boolean isEquivalent(Map<String, Object> configuration) { Boolean httpUpgradeEnabled = ConfigurationHelper.getBooleanProperty(TransportConstants.HTTP_UPGRADE_ENABLED_PROP_NAME, TransportConstants.DEFAULT_HTTP_UPGRADE_ENABLED, configuration); if (httpUpgradeEnabled) { // we need to look at the activemqServerName to distinguish between ActiveMQ servers that could be proxied behind the same // HTTP upgrade handler in the Web server String otherActiveMQServerName = ConfigurationHelper.getStringProperty(TransportConstants.ACTIVEMQ_SERVER_NAME, null, configuration); String activeMQServerName = ConfigurationHelper.getStringProperty(TransportConstants.ACTIVEMQ_SERVER_NAME, null, this.configuration); boolean equivalent = isSameHostAndPort(configuration) && otherActiveMQServerName != null && otherActiveMQServerName.equals(activeMQServerName); return equivalent; } else { return isSameHostAndPort(configuration); } }
@Override public final void checkFlushBatchBuffer() { if (this.batchingEnabled) { //perform the flush only if necessary final int batchBufferSize = batchBufferSize(this.channel, this.writeBufferHighWaterMark); if (batchBufferSize > 0) { this.channel.flush(); } } }
@Override public final void write(ActiveMQBuffer buffer, final boolean flush, final boolean batched) { write(buffer, flush, batched, null); }
@Override public final String toString() { return super.toString() + "[ID=" + getID() + ", local= " + channel.localAddress() + ", remote=" + channel.remoteAddress() + "]"; }
private ChannelFuture writeBatch(final ByteBuf bytes, final int readableBytes, final ChannelPromise promise) { final int batchBufferSize = batchBufferSize(channel, this.writeBufferHighWaterMark); final int nextBatchSize = batchBufferSize + readableBytes; if (nextBatchSize > batchLimit) { //request to flush before writing, to create the chance to make the channel writable again channel.flush(); //let netty use its write batching ability return channel.write(bytes, promise); } else if (nextBatchSize == batchLimit) { return channel.writeAndFlush(bytes, promise); } else { //let netty use its write batching ability return channel.write(bytes, promise); } }