Example simple-mdb can be browsed at https://github.com/apache/tomee/tree/master/examples/simple-mdb

Similar documents
Example schedule-expression can be browsed at

Example custom-injection can be browsed at

Example injection-of-env-entry can be browsed at

Simple Stateless with Descriptor

Alternate Descriptors

Dynamic Implementation

Java TM. Message-Driven Beans. Jaroslav Porubän 2007

Injection Of Datasource


Dynamic Datasource Routing

@Asynchronous Methods

bean-validation-design-by-contract

Example cdi-request-scope can be browsed at

ClassLevelInterceptorOne

Component Interfaces

Injection Of Entitymanager

CDI Produces Disposes

Module 11 Developing Message-Driven Beans

Example ear-testing can be browsed at

What s up with JMS 2.1?

EJB applications guided by tests Jakub Marchwicki

Testing Transactions BMT

Oracle Exam 1z0-895 Java EE 6 Enterprise JavaBeans Developer Certified Expert Exam Version: 14.0 [ Total Questions: 90 ]

Webservice Inheritance

Communication Technologies MoM JMS.NET. Part VI. Message-Oriented Middleware

Arquillian Persistence Extension

Dynamic DAO Implementation

EJB 2.1 CMP EntityBeans (CMP2)

Asynchrone Kommunikation mit Message Driven Beans

Example rest-on-ejb can be browsed at

The Java EE 6 Tutorial

EJB - DEPENDENCY INJECTION

J2EE. Enterprise Architecture Styles: Two-Tier Architectures:

Example Purchase request JMS & MDB. Example Purchase request. Agenda. Purpose. Solution. Enterprise Application Development using J2EE

Module 10 Developing Java EE Applications using Messaging

Asynchronous Messaging. Benoît Garbinato

Example cdi-produces-field can be browsed at

Custom resources in an EAR archive

Distributed Systems. Messaging and JMS Distributed Systems 1. Master of Information System Management

Using Message Driven Beans.

Arquillian & ShrinkWrap

Java Message System. Petr Adámek. April 11 th 2016

Developing a Basic JMS Application

Distributed Systems/Middleware JMS

Web Design and Applications

133 July 23, :01 pm

Solace JMS Integration with JBoss Application Server EAP 6.2

EJB 3.1 vs Contexts and Dependency Injection (CDI) and Dependency Injection for Java in Java EE 6. Jacek Laskowski.

EMBEDDED MESSAGING USING ACTIVEMQ

This is the second part of a multi-article series. For part 1 please see: Dependency Injection in Java EE 6 - Part 1

Java Lounge. Integration Solutions madeeasy ComparisonofJava Integration Frameworks. Mario Goller

Introduction to JMS & Apache ActiveMQ

Enterprise Messaging With ActiveMQ and Spring JMS

@WebService handlers

Figure 1: OpenJMS Integration using GenericJMS RA

Using MQSeries as a Transactional Resource Manager with WebLogic Server. Version 1.0 October 25, 2001

Introduction Abstract. 1.2 Overview. This specification describes the objectives and functionality of the Java TM Message Service (JMS).

FCUBS JMS Configuration Using Websphere Default Messaging Provider Oracle FLEXCUBE Universal Banking Release [February] [2018]

FCUBS JMS Configuration Using Websphere Default Messaging Provider Oracle FLEXCUBE Universal Banking Release [December] [2015]

JBoss to Geronimo - EJB-MDB Migration

EJB Development Using Borland JBuilder 6 and Borland Enterprise Server 5

Red Hat JBoss A-MQ 6.0

RTI Message Service. User s Manual

Amazon MQ. Developer Guide

Compile the source. MDB Single Instance Lab

Red Hat Summit 2009 Jonathan Robie

HornetQ Messaging Developer's Guide

Example rest-xml-json can be browsed at

Using GenericJMSRA with IBMWebSphere MQ

Oracle Fusion Middleware

JavaEE.Next(): Java EE 7, 8, and Beyond

"Charting the Course... Mastering EJB 3.0 Applications. Course Summary

Oracle Banking APIs. Part No. E Third Party Simulation Guide Release April 2018

Basics of programming 3. Java Enterprise Edition

J2EE 1.4. Magnus Larsson. Callista Enterprise AB.

@Stateless. To avoid that use openejb.offline property and set it to true. See Server Configuration for more detail.

Using JNDI from J2EE components

java message service marek konieczny

Red Hat JBoss A-MQ 6.1

Developing JMS Applications for Oracle WebLogic Server c (12.1.3)

Speakers qualifications

WHAT IS EJB. Security. life cycle management.

Vendor: Oracle. Exam Code: 1Z Exam Name: Java Platform, Enterprise Edition 6 Enterprise JavaBeans Developer Certified Expert Exam.

JMS Frequently Asked Questions

Software modular bauen. Ulf Fildebrandt

Angelo Corsaro, Ph.D. Chief Technology Officer! OMG DDS Sig Co-Chair PrismTech

IBM Techdoc: Date last updated: 31-Aug-2016

Red Hat JBoss A-MQ 6.1

CHAPTER 1 FUNDAMENTALS

Java EE 7 Overview and Status. Peter Doschkinow Senior Java Architect

Red Hat JBoss Enterprise Application Platform 7.0

Web Applications 2. Java EE Martin Klíma. WA2 Slide 1

Connecting to Java MQ through JNDI Programmatically.

2.0 Technical Description of the new features

SCBCD EXAM STUDY KIT. Paul Sanghera CX JAVA BUSINESS COMPONENT DEVELOPER CERTIFICATION FOR EJB MANNING. Covers all you need to pass

User Guide. The mom4j development team

Red Hat JBoss Enterprise Application Platform 7.1

53. Interoperability 54. Tools 55. Performance Tuning 56. Configuration Reference

Table of Contents 1.1. Introduction. Legal Notice 1.2. Preface 1.3. Project Info 1.4. Messaging Concepts Architecture 1.7.

For Review Purposes Only. Oracle GlassFish Server 3.1 Application Development Guide

Transcription:

Simple MDB

Example simple-mdb can be browsed at https://github.com/apache/tomee/tree/master/examples/simple-mdb Below is a fun app, a chat application that uses JMS. We create a message driven bean, by marking our class with @MessageDriven. A message driven bean has some similarities with a stateless session bean, in the part that it is pooled too. Well, lets tell our chat-app to listen for incoming messages. That we do by implementing MessageListener and overriding the onmessage(message message). Then this app "listens" for incoming messages, and the messages picked up are processed by onmessage(message message) method. That finishes our message driven bean implementation. The "processing" part could be anything that fits your business-requirement. In this case, it is to respond to the user. The respond method shows how a Message can be sent. This sequence diagram shows how a message is sent. <img src="../../resources/mdb-flow.png" alt=""/> ChatBean package org.superbiz.mdb; import javax.annotation.resource; import javax.ejb.messagedriven; import javax.jms.connection; import javax.jms.connectionfactory; import javax.jms.deliverymode; import javax.jms.jmsexception; import javax.jms.message; import javax.jms.messagelistener; import javax.jms.messageproducer; import javax.jms.queue; import javax.jms.session; import javax.jms.textmessage; @MessageDriven public class ChatBean implements MessageListener { @Resource private ConnectionFactory connectionfactory; 1

@Resource(name = "AnswerQueue") private Queue answerqueue; public void onmessage(message message) { try { final TextMessage textmessage = (TextMessage) message; final String question = textmessage.gettext(); if ("Hello World!".equals(question)) { respond("hello, Test Case!"); else if ("How are you?".equals(question)) { respond("i'm doing well."); else if ("Still spinning?".equals(question)) { respond("once every day, as usual."); catch (JMSException e) { throw new IllegalStateException(e); private void respond(string text) throws JMSException { Connection connection = null; Session session = null; try { connection = connectionfactory.createconnection(); connection.start(); // Create a Session session = connection.createsession(false, Session.AUTO_ACKNOWLEDGE); // Create a MessageProducer from the Session to the Topic or Queue MessageProducer producer = session.createproducer(answerqueue); producer.setdeliverymode(deliverymode.non_persistent); // Create a message TextMessage message = session.createtextmessage(text); // Tell the producer to send the message producer.send(message); finally { // Clean up if (session!= null) session.close(); if (connection!= null) connection.close(); 2

ChatBeanTest package org.superbiz.mdb; import junit.framework.testcase; import javax.annotation.resource; import javax.ejb.embeddable.ejbcontainer; import javax.jms.connection; import javax.jms.connectionfactory; import javax.jms.jmsexception; import javax.jms.messageconsumer; import javax.jms.messageproducer; import javax.jms.queue; import javax.jms.session; import javax.jms.textmessage; public class ChatBeanTest extends TestCase { @Resource private ConnectionFactory connectionfactory; @Resource(name = "ChatBean") private Queue questionqueue; @Resource(name = "AnswerQueue") private Queue answerqueue; public void test() throws Exception { EJBContainer.createEJBContainer().getContext().bind("inject", this); final Connection connection = connectionfactory.createconnection(); connection.start(); final Session session = connection.createsession(false, Session.AUTO_ACKNOWLEDGE); final MessageProducer questions = session.createproducer(questionqueue); final MessageConsumer answers = session.createconsumer(answerqueue); sendtext("hello World!", questions, session); assertequals("hello, Test Case!", receivetext(answers)); 3

sendtext("how are you?", questions, session); assertequals("i'm doing well.", receivetext(answers)); sendtext("still spinning?", questions, session); assertequals("once every day, as usual.", receivetext(answers)); private void sendtext(string text, MessageProducer questions, Session session) throws JMSException { questions.send(session.createtextmessage(text)); private String receivetext(messageconsumer answers) throws JMSException { return ((TextMessage) answers.receive(1000)).gettext(); Running ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.superbiz.mdb.chatbeantest Apache OpenEJB 4.0.0-beta-1 build: 20111002-04:06 http://tomee.apache.org/ INFO - openejb.home = /Users/dblevins/examples/simple-mdb INFO - openejb.base = /Users/dblevins/examples/simple-mdb INFO - Using 'javax.ejb.embeddable.ejbcontainer=true' INFO - Configuring Service(id=Default Security Service, type=securityservice, provider-id=default Security Service) INFO - Configuring Service(id=Default Transaction Manager, type=transactionmanager, provider-id=default Transaction Manager) INFO - Found EjbModule in classpath: /Users/dblevins/examples/simplemdb/target/classes INFO - Beginning load: /Users/dblevins/examples/simple-mdb/target/classes INFO - Configuring enterprise application: /Users/dblevins/examples/simple-mdb WARN - Method 'lookup' is not available for 'javax.annotation.resource'. Probably using an older Runtime. INFO - Auto-configuring a message driven bean ChatBean destination ChatBean to be destinationtype javax.jms.queue INFO - Configuring Service(id=Default MDB Container, type=container, provider- 4

id=default MDB Container) INFO - Auto-creating a container for bean ChatBean: Container(type=MESSAGE, id=default MDB Container) INFO - Configuring Service(id=Default JMS Resource Adapter, type=resource, providerid=default JMS Resource Adapter) INFO - Configuring Service(id=Default JMS Connection Factory, type=resource, providerid=default JMS Connection Factory) INFO - Auto-creating a Resource with id 'Default JMS Connection Factory' of type 'javax.jms.connectionfactory for 'ChatBean'. INFO - Auto-linking resource-ref 'java:comp/env/org.superbiz.mdb.chatbean/connectionfactory' in bean ChatBean to Resource(id=Default JMS Connection Factory) INFO - Configuring Service(id=AnswerQueue, type=resource, provider-id=default Queue) INFO - Auto-creating a Resource with id 'AnswerQueue' of type 'javax.jms.queue for 'ChatBean'. INFO - Auto-linking resource-env-ref 'java:comp/env/answerqueue' in bean ChatBean to Resource(id=AnswerQueue) INFO - Configuring Service(id=ChatBean, type=resource, provider-id=default Queue) INFO - Auto-creating a Resource with id 'ChatBean' of type 'javax.jms.queue for 'ChatBean'. INFO - Configuring Service(id=Default Managed Container, type=container, providerid=default Managed Container) INFO - Auto-creating a container for bean org.superbiz.mdb.chatbeantest: Container(type=MANAGED, id=default Managed Container) INFO - Auto-linking resource-ref 'java:comp/env/org.superbiz.mdb.chatbeantest/connectionfactory' in bean org.superbiz.mdb.chatbeantest to Resource(id=Default JMS Connection Factory) INFO - Auto-linking resource-env-ref 'java:comp/env/answerqueue' in bean org.superbiz.mdb.chatbeantest to Resource(id=AnswerQueue) INFO - Auto-linking resource-env-ref 'java:comp/env/chatbean' in bean org.superbiz.mdb.chatbeantest to Resource(id=ChatBean) INFO - Enterprise application "/Users/dblevins/examples/simple-mdb" loaded. INFO - Assembling app: /Users/dblevins/examples/simple-mdb INFO - Jndi(name="java:global/EjbModule1515710343/org.superbiz.mdb.ChatBeanTest!org.superbiz. mdb.chatbeantest") INFO - Jndi(name="java:global/EjbModule1515710343/org.superbiz.mdb.ChatBeanTest") INFO - Created Ejb(deployment-id=org.superbiz.mdb.ChatBeanTest, ejbname=org.superbiz.mdb.chatbeantest, container=default Managed Container) INFO - Created Ejb(deployment-id=ChatBean, ejb-name=chatbean, container=default MDB Container) INFO - Started Ejb(deployment-id=org.superbiz.mdb.ChatBeanTest, ejbname=org.superbiz.mdb.chatbeantest, container=default Managed Container) INFO - Started Ejb(deployment-id=ChatBean, ejb-name=chatbean, container=default MDB Container) INFO - Deployed Application(path=/Users/dblevins/examples/simple-mdb) Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.547 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 5

6