1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
|
/*
* 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.
*/
package org.jboss.test.remoting.callback.pull.blocking;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
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.Callback;
import org.jboss.remoting.callback.CallbackPoller;
import org.jboss.remoting.callback.HandleCallbackException;
import org.jboss.remoting.callback.InvokerCallbackHandler;
import org.jboss.remoting.transport.Connector;
import org.jboss.remoting.transport.PortUtil;
/**
* Tests the blocking mode for pull callbacks.
*
* See JBREM-641.
*
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
* @version $Revision: 3015 $
* <p>
* Copyright May 2, 2007
* </p>
*/
public class BlockingPullCallbackTestCase extends TestCase
{
public static int port;
private static Logger log = Logger.getLogger(BlockingPullCallbackTestCase.class);
private static final String INVOCATION_TEST = "invocationTest";
private static final String CALLBACK_TEST = "callbackTest";
private static final String COUNTER = "counter";
private static final String CALLBACK_DELAY_KEY = "callbackDelay";
private static boolean firstTime = true;
// remoting server connector
private Connector connector;
private InvokerLocator serverLocator;
private SampleInvocationHandler invocationHandler;
/**
* Sets up target remoting server.
*/
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);
}
}
/**
* Shuts down the server
*/
public void tearDown()
{
if (connector != null)
{
connector.stop();
connector.destroy();
}
}
/**
* Tests blocking and nonblocking direct calls to Client.getCallbacks().
*/
public void testBlockingPullCallback() throws Throwable
{
log.info("entering " + getName());
int CALLBACK_DELAY = 5000;
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
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 SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
client.addListener(callbackHandler);
log.info("client added callback handler for pull callbacks");
// Test for good connection.
Integer count = new Integer(17);
HashMap metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Test nonblocking callbacks.
HashMap pullerMetadata = new HashMap();
pullerMetadata.put(CALLBACK_DELAY_KEY, Integer.toString(CALLBACK_DELAY));
pullerMetadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.NONBLOCKING);
CallbackPuller puller = new CallbackPuller(client, callbackHandler, pullerMetadata);
puller.start();
Thread.sleep(2000);
// Should have received empty list of callbacks.
assertTrue(puller.done);
assertNotNull(puller.callbacks);
assertTrue(puller.callbacks.isEmpty());
// Drain stored callback.
Thread.sleep(CALLBACK_DELAY);
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.NONBLOCKING);
List callbacks = client.getCallbacks(callbackHandler, metadata);
assertNotNull(callbacks);
assertEquals(1, callbacks.size());
// Test blocking callbacks.
pullerMetadata.clear();
pullerMetadata.put(CALLBACK_DELAY_KEY, Integer.toString(CALLBACK_DELAY));
pullerMetadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
pullerMetadata.put(ServerInvoker.BLOCKING_TIMEOUT, Integer.toString(CALLBACK_DELAY * 2));
puller = new CallbackPuller(client, callbackHandler, pullerMetadata);
puller.start();
Thread.sleep(2000);
assertFalse(puller.done);
// Should not have returned from getCallbacks() yet.
assertNull(puller.callbacks);
Thread.sleep(CALLBACK_DELAY);
assertTrue(puller.done);
assertNotNull(puller.callbacks);
assertEquals(1, puller.callbacks.size());
client.removeListener(callbackHandler);
client.disconnect();
}
/**
* Tests configuration of blocking timeout in server configuration map.
*/
public void testBlockingPullCallbackServerConfiguration() throws Throwable
{
log.info("entering " + getName());
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
serverLocator = new InvokerLocator(locatorURI);
log.info("Starting remoting server with locator uri of: " + locatorURI);
HashMap config = new HashMap();
config.put(InvokerLocator.FORCE_REMOTE, "true");
int blockingTimeout = ServerInvoker.DEFAULT_BLOCKING_TIMEOUT + 2000;
config.put(ServerInvoker.BLOCKING_TIMEOUT, Integer.toString(blockingTimeout));
addExtraServerConfig(config);
connector = new Connector(serverLocator, config);
connector.create();
invocationHandler = new SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
client.addListener(callbackHandler);
log.info("client added callback handler for pull callbacks");
// Test for good connection.
Integer count = new Integer(17);
HashMap metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Test blocking callbacks.
//
// Set CALLBACK_DELAY == default blocking timeout + 1000. If the default blocking
// timeout were in effect, Client.getCallbacks() would time out and return without
// getting a callback.
int CALLBACK_DELAY = blockingTimeout - 1000;
Map pullerMetadata = new HashMap();
pullerMetadata.put(CALLBACK_DELAY_KEY, Integer.toString(CALLBACK_DELAY));
pullerMetadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
CallbackPuller puller = new CallbackPuller(client, callbackHandler, pullerMetadata);
puller.start();
Thread.sleep(CALLBACK_DELAY - 1000);
// Callback has not been created yet, so should not have returned from getCallbacks().
assertFalse(puller.done);
assertNull(puller.callbacks);
Thread.sleep(2000);
// Callback has been created, so should have returned from getCallbacks().
assertTrue(puller.done);
assertNotNull(puller.callbacks);
assertEquals(1, puller.callbacks.size());
client.removeListener(callbackHandler);
client.disconnect();
}
/**
* Tests configuration of blocking timeout by Client.addListener() metadata.
* In this case, a CallbackPoller is created.
*/
public void testBlockingCallbackPollerInitialMetadataConfiguration() throws Throwable
{
log.info("entering " + getName());
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
serverLocator = new InvokerLocator(locatorURI);
log.info("Starting remoting server with locator uri of: " + locatorURI);
HashMap config = new HashMap();
config.put(InvokerLocator.FORCE_REMOTE, "true");
int serverBlockingTimeout = ServerInvoker.DEFAULT_BLOCKING_TIMEOUT + 2000;
config.put(ServerInvoker.BLOCKING_TIMEOUT, Integer.toString(serverBlockingTimeout));
addExtraServerConfig(config);
connector = new Connector(serverLocator, config);
connector.create();
invocationHandler = new SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
// Test for good connection.
Integer count = new Integer(17);
HashMap metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Test blocking callbacks.
//
// Reset blocking timeout to server blocking timeout + 2000, and set
// CALLBACK_DELAY to server blocking timeout + 1000. If the server's blocking
// timeout were in effect, Client.getCallbacks() would time out and return without
// getting a callback.
int clientBlockingTimeout = serverBlockingTimeout + 2000;
int CALLBACK_DELAY = serverBlockingTimeout + 1000;
metadata.clear();
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
metadata.put(ServerInvoker.BLOCKING_TIMEOUT, Integer.toString(clientBlockingTimeout));
SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
client.addListener(callbackHandler, metadata);
log.info("client added callback handler for pull callbacks");
// Create callback.
CallbackCreator creator = new CallbackCreator(client, CALLBACK_DELAY);
creator.start();
Thread.sleep(CALLBACK_DELAY - 1000);
// Callback has not been created yet, so should not have returned from getCallbacks().
assertEquals(0, callbackHandler.callbacks.size());
Thread.sleep(2000);
// Callback has been created, so should have returned from getCallbacks().
assertEquals(1, callbackHandler.callbacks.size());
client.removeListener(callbackHandler);
client.disconnect();
}
/**
* Tests configuration of blocking timeout by Client.getCallbacks() metadata.
*/
public void testBlockingPullCallbackInvocationMetadataConfiguration() throws Throwable
{
log.info("entering " + getName());
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
serverLocator = new InvokerLocator(locatorURI);
log.info("Starting remoting server with locator uri of: " + locatorURI);
HashMap config = new HashMap();
config.put(InvokerLocator.FORCE_REMOTE, "true");
int serverBlockingTimeout = ServerInvoker.DEFAULT_BLOCKING_TIMEOUT + 2000;
config.put(ServerInvoker.BLOCKING_TIMEOUT, Integer.toString(serverBlockingTimeout));
addExtraServerConfig(config);
connector = new Connector(serverLocator, config);
connector.create();
invocationHandler = new SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
client.addListener(callbackHandler);
log.info("client added callback handler for pull callbacks");
// Test for good connection.
Integer count = new Integer(17);
HashMap metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Test blocking callbacks.
//
// Reset blocking timeout to server blocking timeout + 2000, and set
// CALLBACK_DELAY to server blocking timeout + 1000. If the server's blocking
// timeout were in effect, Client.getCallbacks() would time out and return without
// getting a callback.
int clientBlockingTimeout = serverBlockingTimeout + 2000;
int CALLBACK_DELAY = serverBlockingTimeout + 1000;
Map pullerMetadata = new HashMap();
pullerMetadata.put(CALLBACK_DELAY_KEY, Integer.toString(CALLBACK_DELAY));
pullerMetadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
pullerMetadata.put(ServerInvoker.BLOCKING_TIMEOUT, Integer.toString(clientBlockingTimeout));
CallbackPuller puller = new CallbackPuller(client, callbackHandler, pullerMetadata);
puller.start();
Thread.sleep(CALLBACK_DELAY - 1000);
// Callback has not been created yet, so should not have returned from getCallbacks().
assertFalse(puller.done);
assertNull(puller.callbacks);
Thread.sleep(2000);
// Callback has been created, so should have returned from getCallbacks().
assertTrue(puller.done);
assertNotNull(puller.callbacks);
assertEquals(1, puller.callbacks.size());
client.removeListener(callbackHandler);
client.disconnect();
}
/**
* Tests CallbackPoller in nonblocking mode.
*/
public void testNonBlockingCallbackPoller() throws Throwable
{
log.info("entering " + getName());
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
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 SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
int CALLBACK_DELAY = 5000;
SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
Map metadata = new HashMap();
metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, Integer.toString(CALLBACK_DELAY));
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.NONBLOCKING);
client.addListener(callbackHandler, metadata);
log.info("client added callback handler for pull callbacks");
// Test for good connection.
Integer count = new Integer(17);
metadata.clear();
metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Test nonblocking behavior.
Thread.sleep(1000);
// Callback will be created after CallbackPoller's first poll.
CallbackCreator creator = new CallbackCreator(client, CALLBACK_DELAY);
creator.start();
Thread.sleep(CALLBACK_DELAY);
assertEquals(0, callbackHandler.callbacks.size());
Thread.sleep(2000);
// CallbackPoller hasn't made second poll yet.
assertEquals(0, callbackHandler.callbacks.size());
Thread.sleep(CALLBACK_DELAY);
assertEquals(1, callbackHandler.callbacks.size());
client.removeListener(callbackHandler);
client.disconnect();
}
/**
* Tests CallbackPoller in blocking mode.
*/
public void testBlockingCallbackPoller() throws Throwable
{
log.info("entering " + getName());
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
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 SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
int CALLBACK_DELAY = 5000;
SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
Map metadata = new HashMap();
metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, Integer.toString(CALLBACK_DELAY));
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
client.addListener(callbackHandler, metadata);
log.info("client added callback handler for pull callbacks");
// Test for good connection.
Integer count = new Integer(17);
metadata.clear();
metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Test blocking behavior.
CallbackCreator creator = new CallbackCreator(client, CALLBACK_DELAY);
creator.start();
assertEquals(0, callbackHandler.callbacks.size());
Thread.sleep(CALLBACK_DELAY + 1000);
assertEquals(1, callbackHandler.callbacks.size());
client.removeListener(callbackHandler);
client.disconnect();
}
/**
* Tests blocking timeout set on server.
*/
public void testBlockingCallbackPollerShutdownServerConfig() throws Throwable
{
log.info("entering " + getName());
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
serverLocator = new InvokerLocator(locatorURI);
log.info("Starting remoting server with locator uri of: " + locatorURI);
HashMap config = new HashMap();
config.put(InvokerLocator.FORCE_REMOTE, "true");
config.put(ServerInvoker.BLOCKING_TIMEOUT, "4000");
addExtraServerConfig(config);
connector = new Connector(serverLocator, config);
connector.create();
invocationHandler = new SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
final Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
// Test for good connection.
Integer count = new Integer(17);
Map metadata = new HashMap();
metadata.clear();
metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Register callback handler.
Field field = Client.class.getDeclaredField("callbackPollers");
field.setAccessible(true);
Map callbackPollers = (Map) field.get(client);
final SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
client.addListener(callbackHandler, metadata);
assertEquals(1, callbackPollers.size());
CallbackPoller callbackPoller = (CallbackPoller) callbackPollers.values().iterator().next();
field = CallbackPoller.class.getDeclaredField("blockingPollerThread");
field.setAccessible(true);
Thread blockingPollerThread = (Thread) field.get(callbackPoller);
assertNotNull(blockingPollerThread);
log.info("client added callback handler for pull callbacks");
// Test blocking timeout.
new Thread()
{
public void run()
{
try
{
client.removeListener(callbackHandler);
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}.start();
Thread.sleep(6000);
assertFalse(blockingPollerThread.isAlive());
client.removeListener(callbackHandler);
client.disconnect();
}
/**
* Tests blocking timeout set on client.
*/
public void testBlockingCallbackPollerShutdownClientConfig() throws Throwable
{
log.info("entering " + getName());
// Start server.
String host = InetAddress.getLocalHost().getHostAddress();
port = PortUtil.findFreePort(host);
String locatorURI = getTransport() + "://" + host + ":" + port;
serverLocator = new InvokerLocator(locatorURI);
log.info("Starting remoting server with locator uri of: " + locatorURI);
HashMap config = new HashMap();
config.put(InvokerLocator.FORCE_REMOTE, "true");
config.put(ServerInvoker.BLOCKING_TIMEOUT, "4000");
addExtraServerConfig(config);
connector = new Connector(serverLocator, config);
connector.create();
invocationHandler = new SampleInvocationHandler();
connector.addInvocationHandler("sample", invocationHandler);
connector.start();
// Create client.
HashMap clientConfig = new HashMap();
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
addExtraClientConfig(clientConfig);
final Client client = new Client(serverLocator, clientConfig);
client.connect();
log.info("client is connected");
// Test for good connection.
Integer count = new Integer(17);
Map metadata = new HashMap();
metadata.clear();
metadata = new HashMap();
metadata.put(COUNTER, count);
Integer response = (Integer) client.invoke(INVOCATION_TEST, metadata);
assertEquals(17, response.intValue());
log.info("client.invoke(INVOCATION_TEST, metadata) successful");
// Register callback handler.
Field field = Client.class.getDeclaredField("callbackPollers");
field.setAccessible(true);
Map callbackPollers = (Map) field.get(client);
final SimpleCallbackHandler callbackHandler = new SimpleCallbackHandler();
metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
metadata.put(ServerInvoker.BLOCKING_TIMEOUT, "10000");
client.addListener(callbackHandler, metadata);
assertEquals(1, callbackPollers.size());
CallbackPoller callbackPoller = (CallbackPoller) callbackPollers.values().iterator().next();
field = CallbackPoller.class.getDeclaredField("blockingPollerThread");
field.setAccessible(true);
Thread blockingPollerThread = (Thread) field.get(callbackPoller);
assertNotNull(blockingPollerThread);
log.info("client added callback handler for pull callbacks");
// Test blocking timeout.
new Thread()
{
public void run()
{
try
{
client.removeListener(callbackHandler);
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}.start();
Thread.sleep(2000);
assertTrue(blockingPollerThread.isAlive());
Thread.sleep(4000);
assertTrue(blockingPollerThread.isAlive());
Thread.sleep(15000);
assertFalse(blockingPollerThread.isAlive());
client.removeListener(callbackHandler);
client.disconnect();
}
protected String getTransport()
{
return "socket";
}
protected void addExtraClientConfig(Map config) {}
protected void addExtraServerConfig(Map config) {}
static class SampleInvocationHandler implements ServerInvocationHandler
{
public Set callbackHandlers = new HashSet();
private int counter = 0;
public void addListener(InvokerCallbackHandler callbackHandler)
{
log.info("Adding callback listener.");
callbackHandlers.add(callbackHandler);
}
public Object invoke(final InvocationRequest invocation) throws Throwable
{
Object payload = invocation.getParameter();
if (INVOCATION_TEST.equals(payload))
{
Map requestMap = invocation.getRequestPayload();
Integer counter = (Integer) requestMap.get(COUNTER);
return counter;
}
else if (CALLBACK_TEST.equals(payload))
{
new Thread()
{
public void run()
{
try
{
Map requestMap = invocation.getRequestPayload();
int delay = Integer.parseInt((String) requestMap.get(CALLBACK_DELAY_KEY));
Thread.sleep(delay);
Iterator it = callbackHandlers.iterator();
while (it.hasNext())
{
InvokerCallbackHandler callbackHandler = (InvokerCallbackHandler) it.next();
log.info("sending callback: " + ++counter);
callbackHandler.handleCallback(new Callback("callback"));
}
log.info("sent callback");
}
catch (HandleCallbackException e)
{
log.error("Unable to send callback");
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}.start();
return null;
}
else
{
throw new Exception("unrecognized invocation: " + payload);
}
}
public void removeListener(InvokerCallbackHandler callbackHandler) {}
public void setMBeanServer(MBeanServer server) {}
public void setInvoker(ServerInvoker invoker) {}
}
static class SimpleCallbackHandler implements InvokerCallbackHandler
{
List callbacks = new ArrayList();
public void handleCallback(Callback callback) throws HandleCallbackException
{
callbacks.add(callback);
log.info("received callback");
}
}
static class CallbackPuller extends Thread
{
Client client;
InvokerCallbackHandler callbackHandler;
Map metadata;
List callbacks;
boolean done;
public CallbackPuller(Client client,
InvokerCallbackHandler callbackHandler,
Map metadata)
{
this.client = client;
this.callbackHandler = callbackHandler;
this.metadata = metadata;
}
public void run()
{
try
{
client.invoke(CALLBACK_TEST, metadata);
log.info("back from client.invoke(CALLBACK_TEST, metadata)");
callbacks = client.getCallbacks(callbackHandler, metadata);
log.info("callbacks count: " + callbacks.size());
done = true;
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
static class CallbackCreator extends Thread
{
Client client;
int delay;
boolean done;
public CallbackCreator(Client client,int delay)
{
this.client = client;
this.delay = delay;
}
public void run()
{
try
{
HashMap metadata = new HashMap();
metadata.put(CALLBACK_DELAY_KEY, Integer.toString(delay));
client.invoke(CALLBACK_TEST, metadata);
log.info("back from client.invoke(CALLBACK_TEST)");
done = true;
}
catch (Throwable e)
{
e.printStackTrace();
}
}
}
}
|