@Override public void close() throws IOException { if (closed) { return; } synchronized (lock) { if (closed) { return; } closed = true; // Don't allow any new request channelAssociation.shutdown(); // First close the channel and connection if (strategy != null) { StreamUtils.safeClose(strategy); strategy = null; } // Cancel all still active operations channelAssociation.shutdownNow(); try { channelAssociation.awaitCompletion(1, TimeUnit.SECONDS); } catch (InterruptedException ignore) { Thread.currentThread().interrupt(); } lock.notifyAll(); } }
/** * Create the protocol client to talk to the remote controller. * * @param channel the remoting channel * @return the client * @throws Exception */ TransactionalProtocolClient createClient(final Channel channel) { channels.add(channel); final ManagementChannelHandler channelAssociation = new ManagementChannelHandler(channel, clientExecutor); final TransactionalProtocolClient client = TransactionalProtocolHandlers.createClient(channelAssociation); channel.addCloseHandler(channelAssociation); channel.receiveMessage(channelAssociation.getReceiver()); return client; }
@Override public ProxyController serverCommunicationRegistered( final String serverProcessName, final ManagementChannelHandler channelAssociation) { if (shutdown || connectionFinished) { throw HostControllerMessages.MESSAGES.hostAlreadyShutdown(); } final String serverName = ManagedServer.getServerName(serverProcessName); final ManagedServer server = servers.get(serverName); if (server == null) { ROOT_LOGGER.noServerAvailable(serverName); return null; } try { final TransactionalProtocolClient client = server.channelRegistered(channelAssociation); final Channel channel = channelAssociation.getChannel(); channel.addCloseHandler( new CloseHandler<Channel>() { public void handleClose(final Channel closed, final IOException exception) { final boolean shuttingDown = shutdown || connectionFinished; // Unregister right away if (server.callbackUnregistered(client, shuttingDown)) { domainController.unregisterRunningServer(server.getServerName()); } } }); return server.getProxyController(); } catch (IOException e) { throw new RuntimeException(e); } }
@Override protected ProxyController createProxyController( final ModelController proxiedController, final PathAddress proxyNodeAddress) { try { channels = new RemoteChannelPairSetup(); channels.setupRemoting( new ManagementChannelInitialization() { @Override public ManagementChannelHandler startReceiving(Channel channel) { final ManagementChannelHandler support = new ManagementChannelHandler(channel, channels.getExecutorService()); support.addHandlerFactory( new TransactionalProtocolOperationHandler(proxiedController, support)); channel.addCloseHandler( new CloseHandler<Channel>() { @Override public void handleClose(Channel closed, IOException exception) { support.shutdownNow(); } }); channel.receiveMessage(support.getReceiver()); return support; } }); channels.startClientConnetion(); } catch (Exception e) { throw new RuntimeException(e); } final Channel clientChannel = channels.getClientChannel(); final ManagementChannelHandler support = new ManagementChannelHandler(clientChannel, channels.getExecutorService()); final RemoteProxyController proxyController = RemoteProxyController.create( support, proxyNodeAddress, ProxyOperationAddressTranslator.SERVER); clientChannel.addCloseHandler( new CloseHandler<Channel>() { @Override public void handleClose(Channel closed, IOException exception) { support.shutdownNow(); } }); clientChannel.receiveMessage(support.getReceiver()); return proxyController; }
@Override public void connectionOpened(final Connection connection) throws IOException { final Channel channel = openChannel(connection, CHANNEL_SERVICE_TYPE, configuration.getOptionMap()); if (setChannel(channel)) { channel.receiveMessage(channelHandler.getReceiver()); channel.addCloseHandler(channelHandler); try { // Start the registration process channelHandler.executeRequest(new RegisterHostControllerRequest(), null).getResult().get(); } catch (Exception e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } throw new IOException(e); } // Registered registered(); } else { channel.closeAsync(); } }
@Override public Channel.Key startReceiving(final Channel channel) { final ManagementChannelHandler handler = new ManagementChannelHandler( ManagementClientChannelStrategy.create(channel), getExecutor()); // Assemble the request handlers for the domain channel handler.addHandlerFactory( new HostControllerRegistrationHandler(handler, getController(), domainController)); handler.addHandlerFactory(new ModelControllerClientOperationHandler(getController(), handler)); handler.addHandlerFactory(new MasterDomainControllerOperationHandlerImpl(domainController)); final Channel.Key key = channel.addCloseHandler( new CloseHandler<Channel>() { @Override public void handleClose(Channel closed, IOException exception) { handler.shutdown(); boolean interrupted = false; try { if (!handler.awaitCompletion(CHANNEL_SHUTDOWN_TIMEOUT, TimeUnit.MILLISECONDS)) { ROOT_LOGGER.gracefulManagementChannelHandlerShutdownTimedOut( CHANNEL_SHUTDOWN_TIMEOUT); } } catch (InterruptedException e) { interrupted = true; ROOT_LOGGER.serviceShutdownIncomplete(e); } catch (Exception e) { ROOT_LOGGER.serviceShutdownIncomplete(e); } finally { handler.shutdownNow(); if (interrupted) { Thread.currentThread().interrupt(); } } } }); channel.receiveMessage(handler.getReceiver()); return key; }
synchronized TransactionalProtocolClient channelRegistered( final ManagementChannelHandler channelAssociation) { final InternalState current = this.internalState; // Create the remote controller client channelAssociation .getAttachments() .attach(TransactionalProtocolClient.SEND_SUBJECT, Boolean.TRUE); final TransactionalProtocolClient remoteClient = TransactionalProtocolHandlers.createClient(channelAssociation); if (current == InternalState.RELOADING) { internalSetState( new TransitionTask() { @Override public boolean execute(ManagedServer server) throws Exception { // Update the current remote connection protocolClient.connected(remoteClient); // clear reload required state requiresReload = false; return true; } }, InternalState.RELOADING, InternalState.SERVER_STARTING); } else { internalSetState( new TransitionTask() { @Override public boolean execute(final ManagedServer server) throws Exception { // Update the current remote connection protocolClient.connected(remoteClient); return true; } // TODO we just check that we are in the correct state, perhaps introduce a new state }, InternalState.SEND_STDIN, InternalState.SERVER_STARTING); } return remoteClient; }
@Override public void close() throws IOException { synchronized (this) { try { if (isConnected()) { try { channelHandler .executeRequest(new UnregisterModelControllerRequest(), null) .getResult() .await(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } finally { try { connectionManager.shutdown(); } finally { super.close(); } } } }
@Override public ManagementChannelHandler startReceiving(final Channel channel) { final ManagementChannelHandler handler = new ManagementChannelHandler( ManagementClientChannelStrategy.create(channel), getExecutor()); // Assemble the request handlers for the domain channel handler.addHandlerFactory( new HostControllerRegistrationHandler( handler, domainController, operationExecutor, getExecutor(), runtimeIgnoreTransformationRegistry)); handler.addHandlerFactory(new ModelControllerClientOperationHandler(getController(), handler)); handler.addHandlerFactory( new MasterDomainControllerOperationHandlerImpl(domainController, getExecutor())); handler.addHandlerFactory(pongRequestHandler); handler.addHandlerFactory( new DomainTransactionalProtocolOperationHandler(txOperationExecutor, handler)); channel.receiveMessage(handler.getReceiver()); return handler; }
@Override public int hashCode() { return remoteConnectionId == null ? Integer.MIN_VALUE : channelHandler.hashCode(); }