Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2012, Red Hat, Inc., and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
  * the License, or (at your option) any later version.
  *
  * This software is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this software; if not, write to the Free
  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  */
 
 package org.jboss.as.ejb3.remote;
 
 

Author(s):
Jaikiran Pai
 
 
     private static final Logger logger = Logger.getLogger(RemotingConnectionClusterNodeManager.class);
 
     private final String nodeName;
     private final ClusterContext clusterContext;
     private final String destinationHost;
     private final int destinationPort;
     private final Endpoint endpoint;
 
 
     RemotingConnectionClusterNodeManager(final ClusterContext clusterContextfinal Endpoint endpoint,
                                          final String nodeNamefinal String destinationHost,
                                          final int destinationPort) {
         this. = nodeName;
         this. = clusterContext;
         this. = destinationHost;
         this. = destinationPort;
         this. = endpoint;
     }
 
     @Override
     public String getNodeName() {
         return this.;
     }
 
     @Override
     public EJBReceiver getEJBReceiver() {
         Connection connection;
         final ReconnectHandler reconnectHandler;
         OptionMap channelCreationOptions = .;
         final EJBClientConfiguration ejbClientConfiguration = this..getEJBClientContext().getEJBClientConfiguration();
         try {
             // if the client configuration is available create the connection using those configs
             if (ejbClientConfiguration != null) {
                 final EJBClientConfiguration.ClusterConfiguration clusterConfiguration = ejbClientConfiguration.getClusterConfiguration(.getClusterName());
                 if (clusterConfiguration == null) {
                     // use default configurations
                     final OptionMap connectionCreationOptions = .;
                     final CallbackHandler callbackHandler = ejbClientConfiguration.getCallbackHandler();
                     final IoFuture<ConnectionfutureConnection = NetworkUtil.connect(nullconnectionCreationOptionscallbackHandlernull);
                     // wait for the connection to be established
                     connection = IoFutureHelper.get(futureConnection, 5000, .);
                     // create a re-connect handler (which will be used on connection breaking down)
                     reconnectHandler = new ClusterNodeReconnectHandler(connectionCreationOptionscallbackHandlerchannelCreationOptions, 5000);
 
                 } else {
                     final EJBClientConfiguration.ClusterNodeConfiguration clusterNodeConfiguration = clusterConfiguration.getNodeConfiguration(this.getNodeName());
                     // use the specified configurations
                     channelCreationOptions = clusterNodeConfiguration == null ? clusterConfiguration.getChannelCreationOptions() : clusterNodeConfiguration.getChannelCreationOptions();
                     final OptionMap connectionCreationOptions = clusterNodeConfiguration == null ? clusterConfiguration.getConnectionCreationOptions() : clusterNodeConfiguration.getConnectionCreationOptions();
                     final CallbackHandler callbackHandler = clusterNodeConfiguration == null ? clusterConfiguration.getCallbackHandler() : clusterNodeConfiguration.getCallbackHandler();
                     final IoFuture<ConnectionfutureConnection = NetworkUtil.connect(nullconnectionCreationOptionscallbackHandlernull);
                     final long timeout = clusterNodeConfiguration == null ? clusterConfiguration.getConnectionTimeout() : clusterNodeConfiguration.getConnectionTimeout();
                    // wait for the connection to be established
                    connection = IoFutureHelper.get(futureConnectiontimeout.);
                    // create a re-connect handler (which will be used on connection breaking down)
                    reconnectHandler = new ClusterNodeReconnectHandler(connectionCreationOptionscallbackHandlerchannelCreationOptionstimeout);
                }
            } else {
                // create the connection using defaults
                final OptionMap connectionCreationOptions = .;
                final CallbackHandler callbackHandler = new AnonymousCallbackHandler();
                final IoFuture<ConnectionfutureConnection = NetworkUtil.connect(nullconnectionCreationOptionscallbackHandlernull);
                // wait for the connection to be established
                connection = IoFutureHelper.get(futureConnection, 5000, .);
                // create a re-connect handler (which will be used on connection breaking down)
                reconnectHandler = new ClusterNodeReconnectHandler(connectionCreationOptionscallbackHandlerchannelCreationOptions, 5000);
            }
        } catch (Exception e) {
            .info("Could not create a connection for cluster node " + this. + " in cluster " + .getClusterName(), e);
            return null;
        }
        return new RemotingConnectionEJBReceiver(connectionreconnectHandlerchannelCreationOptions);
    }
    private class ClusterNodeReconnectHandler implements ReconnectHandler {
        private final String destinationHost;
        private final int destinationPort;
        private final OptionMap connectionCreationOptions;
        private final OptionMap channelCreationOptions;
        private final CallbackHandler callbackHandler;
        private final long connectionTimeout;
        ClusterNodeReconnectHandler(final String hostfinal int portfinal OptionMap connectionCreationOptionsfinal CallbackHandler callbackHandlerfinal OptionMap channelCreationOptionsfinal long connectionTimeoutInMillis) {
            this. = host;
            this. = port;
            this. = connectionCreationOptions;
            this. = channelCreationOptions;
            this. = callbackHandler;
            this. = connectionTimeoutInMillis;
        }
        @Override
        public void reconnect() throws IOException {
            Connection connection = null;
            try {
                final IoFuture<ConnectionfutureConnection = NetworkUtil.connect(nullnull);
                connection = IoFutureHelper.get(futureConnection.);
                .debug("Successfully reconnected to connection " + connection);
            } catch (Exception e) {
                .debug("Failed to re-connect to " + this. + ":" + this.e);
            }
            if (connection == null) {
                return;
            }
            try {
                final EJBReceiver ejbReceiver = new RemotingConnectionEJBReceiver(connectionthis);
                RemotingConnectionClusterNodeManager.this..registerEJBReceiver(ejbReceiver);
            } finally {
                // if we successfully re-connected then unregister this ReconnectHandler from the EJBClientContext
            }
        }
    }
New to GrepCode? Check out our FAQ X