Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2005, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt 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.
 */


Demonstrates Callback acknowledgements.

Author(s):
Ron Sigal

Copyright (c) 2006

 
 package org.jboss.remoting.samples.callback.acknowledgement;
 
 import java.util.List;
 
 
 
 {  
    private static String transport = "socket";
    private static String host;
    private static int port = 5401;
    
    private Client client;

   
   
Can pass transport and port to be used as parameters. Valid transports are 'rmi' and 'socket'.

Parameters:
args
 
    public static void main(String[] args)
    {
       if(args != null && args.length == 2)
       {
           = args[0];
           = Integer.parseInt(args[1]);
       }
       try
       {
           = InetAddress.getLocalHost().getHostName();
       }
       catch (UnknownHostException e1)
       {
          ..println("cannot get local host name");
          return;
       }
       String locatorURI =  + "://" +  + ":" + ;
       CallbackAcknowledgeClient acknowledgeClient = new CallbackAcknowledgeClient();
       try
       {
          acknowledgeClient.createRemotingClient(locatorURI);
 
          acknowledgeClient.testPullCallbackAcknowledgements();
          acknowledgeClient.testPolledCallbackApplicationAcknowledgements();
          acknowledgeClient.testPolledCallbackRemotingAcknowledgements();
          acknowledgeClient.testPushCallbackApplicationAcknowledgements();
          acknowledgeClient.testPushCallbackRemotingAcknowledgements();
          
          acknowledgeClient.disconnectRemotingClient();
       }
       catch(Throwable e)
       {
          e.printStackTrace();
       }
       ..println("done.");
    }
   
   
   public void createRemotingClient(String locatorURIthrows Exception
   {
       = new Client(new InvokerLocator(locatorURI));
      .connect();
   }
   
   
   public void disconnectRemotingClient()
   {
      if ( != null)
         .disconnect();
   }
   
   
   
In this test, the connection is configured for pull callbacks, and acknowledgements are made by an explicit call to Client.acknowledgeCallback() after the callbacks have been received.
   {
      try
      {
         // Register callback handler.
         InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
         .addListener(callbackHandler);
         
         // Request callbacks from server.
         
         // Get callbacks.
         List callbacks = .getCallbacks(callbackHandler);
         
         // Create responses.
         ArrayList responses = new ArrayList(callbacks.size());
         Iterator it = callbacks.iterator();
         while (it.hasNext())
         {
            Callback callback = (Callbackit.next();
            ..println("received pull callback: " + callback.getParameter());
            responses.add(callback.getParameter() + ": acknowledged");
         }
         
         // Acknowledge callbacks.
         .acknowledgeCallbacks(callbackHandlercallbacksresponses);
         
         // Unregister callback handler.
         .removeListener(callbackHandler);
      }
      catch (Throwable e)
      {
         ..println("failure: " + e.getMessage());
      }
   }
   
   
   
In this test the connection is configured for push callbacks implemented in Remoting by polling the server for callbacks and pushing them (on the client side) to the InvokerCallbackHandler. Acknowledgements are made from TestCallbackHandler.handleCallback() by an explicit call to Client.acknowledgeCallback() after the callbacks have been received.
   {
      try
      {
         // Register callback handler.
         InvokerCallbackHandler callbackHandler = new AcknowledgingCallbackHandler();
         HashMap metadata = new HashMap();
         metadata.put(."1000");
         .addListener(callbackHandlermetadata);
         
         // Request callbacks from server.
         Thread.sleep(2000);
         
         // Unregister callback handler.
         .removeListener(callbackHandler);
      }
      catch (Throwable e)
      {
         ..println("failure: " + e.getMessage());
      }
   }
   
   
   
In this test the connection is configured for push callbacks implemented in Remoting by polling the server for callbacks and pushing them (on the client side) to the InvokerCallbackHandler. Acknowledgements are handled implicitly by Remoting.
   {
      try
      {
         // Register callback handler.
         InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
         HashMap metadata = new HashMap();
         metadata.put(."1000");
         .addListener(callbackHandlermetadata);
         
         // Request callbacks from server.
         Thread.sleep(2000);
         
         // Unregister callback handler.
         .removeListener(callbackHandler);
      }
      catch (Throwable e)
      {
         ..println("failure: " + e.getMessage());
      }
   }
   
   
   
In this test the connection is configured for true push callbacks. Acknowledgements are made from TestCallbackHandler.handleCallback() by an explicit call to Client.acknowledgeCallback() after the callbacks have been received.
   {
      try
      {
         // Register callback handler.
         InvokerCallbackHandler callbackHandler = new AcknowledgingCallbackHandler();
         .addListener(callbackHandlernullnulltrue);
         
         // Request callbacks from servrr.
         
         // Unregister callback handler.
         .removeListener(callbackHandler);
      }
      catch (Throwable e)
      {
         ..println("failure: " + e.getMessage());
      }
   }
   
   
   
In this test the connection is configured for true push callbacks, and Acknowledgements are handled implicitly by Remoting.
   {
      try
      {
         // Register callback handler.
         InvokerCallbackHandler callbackHandler = new NonAcknowledgingCallbackHandler();
         .addListener(callbackHandlernullnulltrue);
         
         // Request callbacks from server.
         
         // Unregister callback handler.
         .removeListener(callbackHandler);
      }
      catch (Throwable e)
      {
         ..println("failure: " + e.getMessage());
      }
   }
  
   
   {  
      public void handleCallback(Callback callbackthrows HandleCallbackException
      {
         ..println("received push callback: " + callback.getParameter());
      }
   }
   
   
   {  
      private Client client;
      
      public AcknowledgingCallbackHandler(Client client)
      {
         this. = client;
      }
      
      public void handleCallback(Callback callbackthrows HandleCallbackException
      {
         ..println("received push callback: " + callback.getParameter());
         Object response = callback.getParameter() + ": acknowledged";
         try
         {
            .acknowledgeCallback(thiscallbackresponse);
         }
         catch (Throwable e)
         {
            ..println("Unable to acknowledge callback: " + callback.getParameter());
         }
      }
   }
New to GrepCode? Check out our FAQ X