Send receive

Run
How to run the sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing we need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jms/send-receive/
Now we are ready to start testing. You can run all the tests in this sample by executing:
mvn test
Or you can run individual tests by executing one of the following:
mvn test -Dtest=SyncTestmvn test -Dtest=AsyncTestmvn test -Dtest=ReceptionSynchronizerTest

SyncTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static WebArchive deploy() {
    return ShrinkWrap.create(WebArchive.class)
            .addClasses(MessageSenderSync.class,
                    MessageReceiverSync.class,
                    ClassicMessageSender.class,
                    ClassicMessageReceiver.class,
                    MessageSenderAppManaged.class,
                    MessageReceiverAppManaged.class,
                    Resources.class);
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testClassicApi() {
    String message = "The test message over JMS 1.1 API";
    classicSender.sendMessage(message);

    assertEquals(message, classicReceiver.receiveMessage());
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testContainerManagedJmsContext() {
    String message = "Test message over container-managed JMSContext";
    simpleSender.sendMessage(message);

    assertEquals(message, simpleReceiver.receiveMessage());
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testAppManagedJmsContext() {
    String message = "The test message over app-managed JMSContext";
    appManagedSender.sendMessage(message);

    assertEquals(message, appManagedReceiver.receiveMessage());
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testMultipleSendAndReceive() {
    simpleSender.sendMessage("1");
    simpleSender.sendMessage("2");
    assertEquals("1", simpleReceiver.receiveMessage());
    assertEquals("2", simpleReceiver.receiveMessage());
    simpleSender.sendMessage("3");
    simpleSender.sendMessage("4");
    simpleSender.sendMessage("5");
    assertEquals("3", simpleReceiver.receiveMessage());
    assertEquals("4", simpleReceiver.receiveMessage());
    assertEquals("5", simpleReceiver.receiveMessage());
}

AsyncTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the deployment. Add some javadoc to the @Deployment method. Show me how!
@Deployment
public static WebArchive deploy() {
    return ShrinkWrap.create(WebArchive.class)
            .addClass(MessageSenderAsync.class)
            .addClass(Resources.class)
            .addClass(MessageReceiverAsync.class)
            .addClass(ReceptionSynchronizer.class)
            .addAsWebInfResource(new File("src/test/resources/WEB-INF/ejb-jar.xml"));
}
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testAsync() throws Exception {
    asyncSender.sendMessage("Fire!");
    ReceptionSynchronizer.waitFor(MessageReceiverAsync.class, "onMessage");
    // unless we timed out, the test passes
}

ReceptionSynchronizerTest

Missing a description for the story. Add some javadoc to the TestClass. Show me how!
Missing a description for the test scenario. Add some javadoc to the @Test method. Show me how!
@Test
public void testWaiting() throws NoSuchMethodException, InterruptedException {
    final ReceptionSynchronizer cut = new ReceptionSynchronizer();
    ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
    final Method method = ReceptionSynchronizerTest.class.getDeclaredMethod("testWaiting");
    long startTime = System.currentTimeMillis();
    pool.schedule(new Runnable() {

        @Override
        public void run() {
            cut.registerInvocation(method);
        }
    }, 1, TimeUnit.SECONDS);
    ReceptionSynchronizer.waitFor(ReceptionSynchronizerTest.class, "testWaiting");
    long waitTime = System.currentTimeMillis()-startTime;
    assertTrue("Waited more than 950ms", waitTime > 950);
    assertTrue("Waited no longer than 1050ms", waitTime < 1050);
}

Share the Knowledge

Find this sample useful? Share on

There's a lot more about JavaEE to cover. If you're ready to learn more, check out the other available samples.

Help Improve

Find a bug in the sample? Something missing? You can fix it by editing the source, making the correction and sending a pull request. Or report the problem to the issue tracker

Recent Changelog

  • Oct 05, 2014: #252 fixed arquillian configuration for the jobs by John D. Ament
  • Jul 15, 2014: Removed header license. the licensing is now referenced in the license file in the root of the project by Roberto Cortez
  • Mar 20, 2014: Update arquillian.xml cr1 references to final by Aslak Knutsen
  • Jan 11, 2014: Transaction-aware receptionsynchronizer by pdudits
  • Jan 11, 2014: Make jms tests pass in managed wildfly by pdudits
  • Dec 05, 2013: Adding a new test to send/receive multiple messages by arun-gupta
  • Dec 05, 2013: Adding a convenient method to receive all messages (not used any where for now) by arun-gupta
  • Nov 21, 2013: Refactoring some code to be more intuitive - all 5 tests work on wildfly beta2 snapshot and glassfish 4 by Arun Gupta
  • Nov 15, 2013: Removed copyright by Patrik Dudits
  • Nov 14, 2013: Documented jms samples by Patrik Dudits
How to help improve this sample
The source code for this sample can be found in the javaee7-samples GitHub repository. The first thing you need to do is to get the source by downloading the repository and then go into the samples folder:
git clone git://github.com/javaee-samples/javaee7-samples.git
cd javaee7-samples/jms/send-receive/

Do the changes as you see fit and send a pull request!

Good Luck!