1 17 package org.apache.servicemix.jbi.nmr.flow.jms; 18 19 import org.apache.activemq.broker.BrokerService; 20 import org.apache.activemq.xbean.BrokerFactoryBean; 21 import org.apache.servicemix.jbi.container.ActivationSpec; 22 import org.apache.servicemix.jbi.container.JBIContainer; 23 import org.apache.servicemix.jbi.nmr.flow.jms.JMSFlow; 24 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver; 25 import org.apache.servicemix.tck.ReceiverComponent; 26 import org.apache.servicemix.tck.SenderComponent; 27 import org.springframework.core.io.ClassPathResource; 28 29 import junit.framework.TestCase; 30 31 35 public class JMSFlowTest extends TestCase { 36 JBIContainer senderContainer = new JBIContainer(); 37 JBIContainer receiverContainer = new JBIContainer(); 38 private SenderComponent sender; 39 private ReceiverComponent receiver; 40 private static final int NUM_MESSAGES = 10; 41 protected BrokerService broker; 42 43 46 protected void setUp() throws Exception { 47 super.setUp(); 48 49 BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("org/apache/servicemix/jbi/nmr/flow/jca/broker.xml")); 50 bfb.afterPropertiesSet(); 51 broker = bfb.getBroker(); 52 broker.start(); 53 senderContainer.setName("senderContainer"); 54 senderContainer.setFlowName("jms?jmsURL=tcp://localhost:61216"); 55 senderContainer.init(); 56 senderContainer.start(); 57 Object senderFlow = senderContainer.getFlow(); 58 assertTrue(senderFlow instanceof JMSFlow); 59 60 61 receiverContainer.setName("receiverContainer"); 62 receiverContainer.setFlowName("jms?jmsURL=tcp://localhost:61216"); 63 receiverContainer.init(); 64 receiverContainer.start(); 65 Object receiverFlow = receiverContainer.getFlow(); 66 assertTrue(receiverFlow instanceof JMSFlow); 67 68 Thread.sleep(2000); 69 70 receiver = new ReceiverComponent(); 71 sender = new SenderComponent(); 72 sender.setResolver(new ServiceNameEndpointResolver(ReceiverComponent.SERVICE)); 73 } 74 75 protected void tearDown() throws Exception { 76 super.tearDown(); 77 senderContainer.shutDown(); 78 receiverContainer.shutDown(); 79 broker.stop(); 80 } 81 82 public void testInOnly() throws Exception { 83 senderContainer.activateComponent(new ActivationSpec("sender", sender)); 84 receiverContainer.activateComponent(new ActivationSpec("receiver", receiver)); 85 Thread.sleep(1000); 86 87 sender.sendMessages(NUM_MESSAGES); 88 Thread.sleep(3000); 89 receiver.getMessageList().assertMessagesReceived(NUM_MESSAGES); 90 } 91 92 public void testClusteredInOnly() throws Exception { 93 final SenderComponent sender = new SenderComponent(); 94 final ReceiverComponent receiver1 = new ReceiverComponent(); 95 final ReceiverComponent receiver2 = new ReceiverComponent(); 96 sender.setResolver(new ServiceNameEndpointResolver(ReceiverComponent.SERVICE)); 97 98 senderContainer.activateComponent(new ActivationSpec("sender", sender)); 99 senderContainer.activateComponent(new ActivationSpec("receiver", receiver1)); 100 receiverContainer.activateComponent(new ActivationSpec("receiver", receiver2)); 101 Thread.sleep(1000); 102 103 sender.sendMessages(NUM_MESSAGES); 104 Thread.sleep(3000); 105 assertTrue(receiver1.getMessageList().hasReceivedMessage()); 106 assertTrue(receiver2.getMessageList().hasReceivedMessage()); 107 receiver1.getMessageList().flushMessages(); 108 receiver2.getMessageList().flushMessages(); 109 110 senderContainer.deactivateComponent("receiver"); 111 Thread.sleep(1000); 112 113 sender.sendMessages(NUM_MESSAGES); 114 Thread.sleep(3000); 115 assertFalse(receiver1.getMessageList().hasReceivedMessage()); 116 assertTrue(receiver2.getMessageList().hasReceivedMessage()); 117 receiver1.getMessageList().flushMessages(); 118 receiver2.getMessageList().flushMessages(); 119 120 senderContainer.activateComponent(new ActivationSpec("receiver", receiver1)); 121 receiverContainer.deactivateComponent("receiver"); 122 Thread.sleep(1000); 123 124 sender.sendMessages(NUM_MESSAGES); 125 Thread.sleep(3000); 126 assertTrue(receiver1.getMessageList().hasReceivedMessage()); 127 assertFalse(receiver2.getMessageList().hasReceivedMessage()); 128 receiver1.getMessageList().flushMessages(); 129 receiver2.getMessageList().flushMessages(); 130 } 131 132 } 133 | Popular Tags |