Author: ron.sigal(a)jboss.com
Date: 2014-12-10 23:53:16 -0500 (Wed, 10 Dec 2014)
New Revision: 6531
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/callback/pull/memory/blocking/BlockingCallbackStoreSynchronizationTestCase.java
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/BlockingCallbackStore.java
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/ServerInvokerCallbackHandler.java
Log:
JBREM-1327: BlockingCallbackStore can hold multiple Callbacks.
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/BlockingCallbackStore.java
===================================================================
---
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/BlockingCallbackStore.java 2014-12-11
04:39:06 UTC (rev 6530)
+++
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/BlockingCallbackStore.java 2014-12-11
04:53:16 UTC (rev 6531)
@@ -44,9 +44,11 @@
private Object lockObject = new Object();
private List callbacks = new ArrayList();
+ private int maxSize = MAX_SIZE_DEFAULT;
private static final Logger log = Logger.getLogger(BlockingCallbackStore.class);
-
+ private static int MAX_SIZE_DEFAULT = 10000;
+
/**
* Gets the number of callbacks that are waiting
* to be processed.
@@ -96,25 +98,41 @@
synchronized(lockObject)
{
- try
+ while (callbacks.size() >= maxSize)
{
- callbacks.add(object);
- lockObject.wait();
+ try
+ {
+ log.info(this + ": Callback store is full. Waiting for a callback to
be removed.");
+ lockObject.wait();
+ break;
+ }
+ catch (InterruptedException e)
+ {
+ log.debug("InterruptedException received while waiting for thread
(" +
+ Thread.currentThread() + ") to be released from
BlockingCallbackStore.add(Serializable) call.");
+ }
}
- catch (InterruptedException e)
- {
- log.debug("InterruptedException received while waiting for thread
(" +
- Thread.currentThread() + ") to be released from
BlockingCallbackStore.add(Serializable) call.");
- }
+ callbacks.add(object);
}
}
/**
- * No op
* @param config
*/
public void setConfig(Map config)
{
+ String s = (String)
config.get(ServerInvokerCallbackHandler.CALLBACK_STORE_MAX_SIZE);
+ if (s != null)
+ {
+ try
+ {
+ maxSize = Integer.parseInt(s);
+ }
+ catch (NumberFormatException e)
+ {
+ log.warn("Unable to convert value of parameter \"" +
ServerInvokerCallbackHandler.CALLBACK_STORE_MAX_SIZE + "\": " + s + ",
using default value " + MAX_SIZE_DEFAULT);
+ }
+ }
}
/**
Modified:
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/ServerInvokerCallbackHandler.java
===================================================================
---
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/ServerInvokerCallbackHandler.java 2014-12-11
04:39:06 UTC (rev 6530)
+++
remoting2/branches/2.x/src/main/org/jboss/remoting/callback/ServerInvokerCallbackHandler.java 2014-12-11
04:53:16 UTC (rev 6531)
@@ -146,6 +146,8 @@
*/
private Map idToListenerMap = Collections.synchronizedMap(new HashMap());
+ /** The key used to pass to the callback store a maximum size */
+ public static final String CALLBACK_STORE_MAX_SIZE =
"callbackStoreMaxSize";
public ServerInvokerCallbackHandler(InvocationRequest invocation, InvokerLocator
serverLocator,
ServerInvoker owner) throws Exception
Added:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/callback/pull/memory/blocking/BlockingCallbackStoreSynchronizationTestCase.java
===================================================================
---
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/callback/pull/memory/blocking/BlockingCallbackStoreSynchronizationTestCase.java
(rev 0)
+++
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/callback/pull/memory/blocking/BlockingCallbackStoreSynchronizationTestCase.java 2014-12-11
04:53:16 UTC (rev 6531)
@@ -0,0 +1,239 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, 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.
+*/
+package org.jboss.test.remoting.callback.pull.memory.blocking;
+
+import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+
+import junit.framework.TestCase;
+
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.PatternLayout;
+import org.jboss.remoting.Client;
+import org.jboss.remoting.InvocationRequest;
+import org.jboss.remoting.InvokerLocator;
+import org.jboss.remoting.ServerInvocationHandler;
+import org.jboss.remoting.ServerInvoker;
+import org.jboss.remoting.callback.BlockingCallbackStore;
+import org.jboss.remoting.callback.Callback;
+import org.jboss.remoting.callback.HandleCallbackException;
+import org.jboss.remoting.callback.InvokerCallbackHandler;
+import org.jboss.remoting.callback.ServerInvokerCallbackHandler;
+import org.jboss.remoting.transport.Connector;
+import org.jboss.remoting.transport.PortUtil;
+
+
+/**
+ * Unit test for JBREM-????
+ *
+ * @author <a href="ron.sigal(a)jboss.com">Ron Sigal</a>
+ * @version
+ * <p>
+ * Copyright November 19, 2014
+ * </p>
+ */
+public class BlockingCallbackStoreSynchronizationTestCase extends TestCase
+{
+ private static Logger log =
Logger.getLogger(BlockingCallbackStoreSynchronizationTestCase.class);
+
+ private static boolean firstTime = true;
+ private static int NUMBER_OF_CALLBACKS = 100;
+
+ protected String host;
+ protected int port;
+ protected String locatorURI;
+ protected InvokerLocator serverLocator;
+ protected Connector connector;
+ protected TestInvocationHandler invocationHandler;
+ protected InvokerCallbackHandler invokerCallbackHandler;
+ protected BlockingCallbackStore callbackStore;
+
+
+ public void setUp() throws Exception
+ {
+ if (firstTime)
+ {
+ firstTime = false;
+ Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
+ Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
+ String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
+ PatternLayout layout = new PatternLayout(pattern);
+ ConsoleAppender consoleAppender = new ConsoleAppender(layout);
+ Logger.getRootLogger().addAppender(consoleAppender);
+ }
+ }
+
+
+ public void tearDown()
+ {
+ }
+
+
+ public void testBlockingCallbackStore() throws Throwable
+ {
+ log.info("entering " + getName());
+
+ // Start server.
+ setupServer();
+
+ // Create client.
+ InvokerLocator clientLocator = new InvokerLocator(locatorURI);
+ HashMap clientConfig = new HashMap();
+ clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraClientConfig(clientConfig);
+ final Client client = new Client(clientLocator, clientConfig);
+ client.connect();
+ log.info("client is connected");
+
+ // Test connection.
+ assertEquals("test", client.invoke("test"));
+ log.info("connection is good");
+
+ // Add callback handler.
+ TestCallbackHandler testCallbackHandler = new TestCallbackHandler();
+ client.addListener(testCallbackHandler);
+ Field field =
ServerInvokerCallbackHandler.class.getDeclaredField("callbackStore");
+ field.setAccessible(true);
+ callbackStore = (BlockingCallbackStore)
field.get(invocationHandler.callbackHandler);
+ System.out.println("callbackStore.size(): " + callbackStore.size());
+
+ // Create callbacks.
+ new Thread()
+ {
+ public void run()
+ {
+ try
+ {
+ client.invoke("abc");
+ }
+ catch (Throwable e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }.start();
+ Thread.sleep(2000);
+ System.out.println("callbackStore.size(): " + callbackStore.size());
+ assertEquals(NUMBER_OF_CALLBACKS, callbackStore.size());
+
+ // Get first batch of callbacks.
+ List callbacks = client.getCallbacks(testCallbackHandler);
+ System.out.println("callbacks retrieved: " + callbacks.size());
+ assertEquals(NUMBER_OF_CALLBACKS, callbacks.size());
+
+
+ // Get second batch of callbacks.
+ callbacks = client.getCallbacks(testCallbackHandler);
+ System.out.println("callbacks retrieved: " + callbacks.size());
+ assertEquals(10, callbacks.size());
+
+ client.disconnect();
+ shutdownServer();
+ log.info(getName() + " PASSES");
+ }
+
+
+ protected String getTransport()
+ {
+ return "socket";
+ }
+
+
+ protected void addExtraClientConfig(Map config) {}
+ protected void addExtraServerConfig(Map config) {}
+
+
+ protected void setupServer() throws Exception
+ {
+ host = InetAddress.getLocalHost().getHostAddress();
+ port = PortUtil.findFreePort(host);
+ locatorURI = getTransport() + "://" + host + ":" + port;
+ locatorURI += "/?" + ServerInvokerCallbackHandler.CALLBACK_STORE_KEY +
"=" + BlockingCallbackStore.class.getName();
+ locatorURI += "&" +
ServerInvokerCallbackHandler.CALLBACK_STORE_MAX_SIZE + "=" +
NUMBER_OF_CALLBACKS;
+ String metadata = System.getProperty("remoting.metadata");
+ if (metadata != null)
+ {
+ locatorURI += "&" + metadata;
+ }
+ serverLocator = new InvokerLocator(locatorURI);
+ log.info("Starting remoting server with locator uri of: " + locatorURI);
+ HashMap config = new HashMap();
+ config.put(InvokerLocator.FORCE_REMOTE, "true");
+ addExtraServerConfig(config);
+ connector = new Connector(serverLocator, config);
+ connector.create();
+ invocationHandler = new TestInvocationHandler();
+ connector.addInvocationHandler("test", invocationHandler);
+ connector.start();
+ }
+
+
+ protected void shutdownServer() throws Exception
+ {
+ if (connector != null)
+ connector.stop();
+ }
+
+
+ static class TestInvocationHandler implements ServerInvocationHandler
+ {
+ public ServerInvokerCallbackHandler callbackHandler;
+
+ public void addListener(InvokerCallbackHandler callbackHandler)
+ {
+ this.callbackHandler = (ServerInvokerCallbackHandler) callbackHandler;
+ this.callbackHandler.setShouldPersist(true);
+ }
+ public Object invoke(final InvocationRequest invocation) throws Throwable
+ {
+ if ("test".equals(invocation.getParameter()))
+ {
+ return "test";
+ }
+ for (int i = 0; i < NUMBER_OF_CALLBACKS + 10; i++)
+ {
+ callbackHandler.handleCallback(new Callback(new Integer(i)));
+// System.out.println("creating callback " + i);
+ }
+ return "done";
+ }
+ public void removeListener(InvokerCallbackHandler callbackHandler) {}
+ public void setMBeanServer(MBeanServer server) {}
+ public void setInvoker(ServerInvoker invoker) {}
+ }
+
+
+ static class TestCallbackHandler implements InvokerCallbackHandler
+ {
+ public void handleCallback(Callback callback) throws HandleCallbackException
+ {
+ log.info("received callback");
+ }
+ }
+}
\ No newline at end of file
Property changes on:
remoting2/branches/2.x/src/tests/org/jboss/test/remoting/callback/pull/memory/blocking/BlockingCallbackStoreSynchronizationTestCase.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain