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.
 */
 
 /*
  * Created on Oct 4, 2005
  */
  
 package org.jboss.remoting.samples.multiplex.invoker;
 
 
 import java.util.List;
 
 

Author(s):
Ron Sigal
 
 public class MultiplexInvokerServer
 {
    private Connector connector = null;
 
    
    public void init() throws Exception
    {
        = new Connector();
       InvokerLocator locator = new InvokerLocator("multiplex://localhost:9090");
       .setInvokerLocator(locator.getLocatorURI());
       .create();
        = new SampleInvocationHandler();
       .addInvocationHandler("sample");
       .start();
       ..println("Started server at: " + .getInvokerLocator());
    }
 
    
    public boolean isDone()
    {
       return .isDone();
    }
 
    
    protected void setUp() throws Exception
    {
       init();
    }
 
 
    protected void tearDown() throws Exception
    {
       if( != null)
       {
          .stop();
          .destroy();
       }
    }
    
 
    public static void main(String[] args)
    {
       MultiplexInvokerServer test = new MultiplexInvokerServer();
       try
       {
          test.setUp();
          
          while (!test.isDone())
             Thread.sleep(1000);
             
          test.tearDown();
      }
      catch(Exception e)
      {
         e.printStackTrace();
      }
   }
   
 
   
Simple invocation handler implementation. When callback client's are registered, will generate callbacks periodically.
   public static class SampleInvocationHandler implements ServerInvocationHandlerRunnable
   {
      // list of callback listeners registered
      private List listeners = new ArrayList();
      private boolean didCallbacks = false;
      
      public SampleInvocationHandler()
      {
         // will start a new thread for generating callbacks.
         Thread callbackThread = new Thread(this);
         callbackThread.setDaemon(true);
         callbackThread.start();
      }
      
      
called by the remoting server to handle the invocation from client.

Parameters:
invocation
Returns:
Throws:
java.lang.Throwable
      public Object invoke(InvocationRequest invocationthrows Throwable
      {
         // Print out the invocation request
         ..println("Invocation request is: " + invocation.getParameter());
         
         // Return invocation parameter
         return invocation.getParameter();
      }
      
      
Adds a callback handler that will listen for callbacks from the server invoker handler.

Parameters:
callbackHandler
      public void addListener(InvokerCallbackHandler callbackHandler)
      {
         ..println("Adding callback listener.");
         .add(callbackHandler);
      }
      
      
Removes the callback handler that was listening for callbacks from the server invoker handler.

Parameters:
callbackHandler
      public void removeListener(InvokerCallbackHandler callbackHandler)
      {
         ..println("Removing callback listener.");
         .remove(callbackHandler);
      }
      
      
Will generate callback messages every second while shouldGenerateCallbacks flag is true.
      public void run()
      {
         // wait for a callback listener to be registered
         while (.isEmpty())
         {
            try
            {
               Thread.sleep(1000);
            }
            catch(InterruptedException e)
            {
            }
         }
         
         for (int i = 0; i < 2; i++)
         {
            // create new callback message
            Callback callback = new Callback(new Integer(29 * (i + 1)));
            ..println("generating callback value: " + callback.getCallbackObject());
            
            // get a copy of the listener list to avoid ConcurrentModificationException
            List localListenersnew ArrayList();
            
            // send callback to all registered listeners
            Iterator itr = localListeners.iterator();
            while(itr.hasNext())
            {
               InvokerCallbackHandler callbackHandler = (InvokerCallbackHandleritr.next();
               
               try
               {
                  callbackHandler.handleCallback(callback);
               }
               catch(HandleCallbackException e)
               {
                  e.printStackTrace();
               }
               
               try
               {
                  Thread.sleep(2000);
               }
               catch (InterruptedException ignored)
               {
               }
            } 
         }
         
          = true;
      }
      
      
set the mbean server that the handler can reference

Parameters:
server
      public void setMBeanServer(MBeanServer server)
      {
         // NO OP as do not need reference to MBeanServer for this handler
      }
      
      
set the invoker that owns this handler

Parameters:
invoker
      public void setInvoker(ServerInvoker invoker)
      {
         // NO OP as do not need reference back to the server invoker
      }
      
      
      public boolean isDone()
      {
         return ;
      }
   }
New to GrepCode? Check out our FAQ X