|
| 1 | +package org.javaee7.jms.batch; |
| 2 | + |
| 3 | +import org.javaee7.util.BatchTestHelper; |
| 4 | +import org.jboss.arquillian.container.test.api.Deployment; |
| 5 | +import org.jboss.arquillian.junit.Arquillian; |
| 6 | +import org.jboss.arquillian.junit.InSequence; |
| 7 | +import org.jboss.shrinkwrap.api.ArchivePaths; |
| 8 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 9 | +import org.jboss.shrinkwrap.api.asset.EmptyAsset; |
| 10 | +import org.jboss.shrinkwrap.api.spec.WebArchive; |
| 11 | +import org.junit.Test; |
| 12 | +import org.junit.runner.RunWith; |
| 13 | + |
| 14 | +import javax.annotation.Resource; |
| 15 | +import javax.batch.operations.JobOperator; |
| 16 | +import javax.batch.runtime.BatchRuntime; |
| 17 | +import javax.batch.runtime.JobExecution; |
| 18 | +import javax.ejb.EJB; |
| 19 | +import javax.jms.*; |
| 20 | +import java.util.Properties; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +import static org.junit.Assert.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * This test demonstrates programmatical creation of durable consumer, and reading |
| 27 | + * its subscribed messages in a batch job in form of an +ItemReader+. |
| 28 | + * |
| 29 | + * include::JmsItemReader[] |
| 30 | + * |
| 31 | + * The items are then fed into the writer, that performs the aggregation and stores |
| 32 | + * the result into a +@Singleton+ EJB. |
| 33 | + * |
| 34 | + * include::SummingItemWriter[] |
| 35 | + * |
| 36 | + * @author Patrik Dudits |
| 37 | + */ |
| 38 | +@RunWith(Arquillian.class) |
| 39 | +public class JmsItemReaderTest { |
| 40 | + |
| 41 | + /** |
| 42 | + * Upon deployment a topic and connection factory for durable subscription are created: |
| 43 | + * |
| 44 | + * include::Resources[] |
| 45 | + * |
| 46 | + * Then the subscription itself is created by means of +@Singleton+ +@Startup+ EJB |
| 47 | + * +SubscriptionCreator+. |
| 48 | + * |
| 49 | + * include::SubscriptionCreator#createSubscription[] |
| 50 | + * |
| 51 | + * The job itself computes sum and count of random numbers that are send on the topic. |
| 52 | + * Note that at time of sending there is no active consumer listening on the topic. |
| 53 | + */ |
| 54 | + @Deployment |
| 55 | + public static WebArchive deployment() { |
| 56 | + return ShrinkWrap.create(WebArchive.class) |
| 57 | + .addAsWebInfResource(EmptyAsset.INSTANCE, ArchivePaths.create("beans.xml")) |
| 58 | + .addClass(BatchTestHelper.class) |
| 59 | + .addPackage(JmsItemReader.class.getPackage()) |
| 60 | + .addAsResource("META-INF/batch-jobs/jms-job.xml"); |
| 61 | + } |
| 62 | + |
| 63 | + @Resource(lookup = "java:comp/DefaultJMSConnectionFactory") |
| 64 | + ConnectionFactory factory; |
| 65 | + |
| 66 | + @Resource(lookup = Resources.TOPIC) |
| 67 | + Topic topic; |
| 68 | + |
| 69 | + @EJB |
| 70 | + ResultCollector collector; |
| 71 | + |
| 72 | + /** |
| 73 | + * In this test case we verify that the subscription is really created upon deployment |
| 74 | + * and thus messages are waiting for the job even before the first run of it. |
| 75 | + * |
| 76 | + * The subscription is not deleted even after the application is undeployed, because |
| 77 | + * the physical topic and its subscription in the message broker still exist, |
| 78 | + * even after the application scoped managed objects are deleted. |
| 79 | + * |
| 80 | + * Following method is used to generate the payload: |
| 81 | + * |
| 82 | + * include::#sendMessages[] |
| 83 | + * |
| 84 | + * So we send 10 random numbers, and verify that summing integers works exactly the |
| 85 | + * same way on both ends. Or that the job really picked up all the numbers submitted |
| 86 | + * for the computation. |
| 87 | + */ |
| 88 | + @InSequence(1) |
| 89 | + @Test |
| 90 | + public void worksAfterDeployment() throws InterruptedException { |
| 91 | + int sum = sendMessages(10); |
| 92 | + runJob(); |
| 93 | + assertEquals(10, collector.getLastItemCount()); |
| 94 | + assertEquals(sum, collector.getLastSum()); |
| 95 | + assertEquals(1, collector.getNumberOfJobs()); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * To verify that the durable subscription really collects messages we do few |
| 100 | + * more runs. |
| 101 | + */ |
| 102 | + @InSequence(2) |
| 103 | + @Test |
| 104 | + public void worksInMultipleRuns() throws InterruptedException { |
| 105 | + int sum = sendMessages(14); |
| 106 | + runJob(); |
| 107 | + assertEquals(14, collector.getLastItemCount()); |
| 108 | + assertEquals(sum, collector.getLastSum()); |
| 109 | + assertEquals(2, collector.getNumberOfJobs()); |
| 110 | + sum = sendMessages(8); // <1> Sending messages from separate connections makes no difference |
| 111 | + sum += sendMessages(4); |
| 112 | + runJob(); |
| 113 | + assertEquals(12, collector.getLastItemCount()); |
| 114 | + assertEquals(sum, collector.getLastSum()); |
| 115 | + assertEquals(3, collector.getNumberOfJobs()); |
| 116 | + } |
| 117 | + |
| 118 | + private void runJob() throws InterruptedException { |
| 119 | + JobOperator jobOperator = BatchRuntime.getJobOperator(); |
| 120 | + Long executionId = jobOperator.start("jms-job", new Properties()); |
| 121 | + JobExecution jobExecution = jobOperator.getJobExecution(executionId); |
| 122 | + |
| 123 | + BatchTestHelper.keepTestAlive(jobExecution); |
| 124 | + } |
| 125 | + |
| 126 | + private int sendMessages(int count) { |
| 127 | + int sum = 0; |
| 128 | + Random r = new Random(); |
| 129 | + try (JMSContext jms = factory.createContext(Session.AUTO_ACKNOWLEDGE)) { |
| 130 | + JMSProducer producer = jms.createProducer(); |
| 131 | + for (int i=0; i< count; i++) { |
| 132 | + int payload = r.nextInt(); |
| 133 | + producer.send(topic, payload); |
| 134 | + sum += payload; |
| 135 | + } |
| 136 | + } |
| 137 | + return sum; |
| 138 | + } |
| 139 | +} |
0 commit comments