[jboss-user] [EJB 3.0 Users] - Re: EJB3.0 MDB example

rodedh do-not-reply at jboss.com
Tue Nov 3 09:57:04 EST 2009


You can use Message Driven POJOs. 


  | import org.jboss.annotation.ejb.DeliveryMode;
  | import org.jboss.annotation.ejb.MessageProperties;
  | import org.jboss.annotation.ejb.Producer;
  | 
  | @Producer(connectionFactory="java:/JmsXA")
  | @MessageProperties(delivery=DeliveryMode.NON_PERSISTENT, timeToLive=0, priority=1)
  | public interface MyProcessor {
  | 	public void processSomething(Serializable anyParameter);
  | }
  | 

and the implementing class:

  | import java.io.BufferedInputStream;
  | import java.io.File;
  | import java.io.FileOutputStream;
  | import java.io.InputStream;
  | import java.net.URL;
  | import java.net.URLConnection;
  | import java.util.Collection;
  | import java.util.Date;
  | 
  | import javax.ejb.ActivationConfigProperty;
  | import javax.ejb.EJB;
  | import javax.ejb.TransactionAttribute;
  | import javax.ejb.TransactionAttributeType;
  | 
  | import org.jboss.annotation.ejb.Consumer;
  | import org.jboss.annotation.ejb.PoolClass;
  | import org.jboss.logging.Logger;
  | 
  | import com.bulloons.server.dataCollector.omgili.entities.Document;
  | 
  | @Consumer(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"),
  | 		@ActivationConfigProperty(propertyName = "destination", propertyValue = "queue/MyTasksProcess"),
  | 		@ActivationConfigProperty(propertyName = "maxSession", propertyValue = "8"),
  | 		@ActivationConfigProperty(propertyName = "minSession", propertyValue = "1"),
  | 		@ActivationConfigProperty(propertyName = "maxMessages", propertyValue = "1") })
  | @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
  | @PoolClass(value = org.jboss.ejb3.StrictMaxPool.class, maxSize = 8 , timeout = (1000 * 60 * 60))
  | // 10 minutes waiting for a pooled MDB. * 8 Concurrent Threads allowed,
  | public class MyProcessorMDBean implements Downloader {
  | 
  |     public void processSomething(Serializable anyParameter){
  |         //Do the actual processing
  |     }
  | 
  | }
  | 

And a client:


  | import java.io.BufferedReader;
  | import java.io.InputStreamReader;
  | import java.net.URL;
  | import java.util.ArrayList;
  | import java.util.Collection;
  | import java.util.Date;
  | import java.util.List;
  | import java.util.regex.Matcher;
  | import java.util.regex.Pattern;
  | 
  | import javax.annotation.PostConstruct;
  | import javax.annotation.Resource;
  | import javax.ejb.EJBException;
  | import javax.ejb.SessionContext;
  | import javax.ejb.Stateless;
  | import javax.jms.JMSException;
  | import javax.naming.InitialContext;
  | 
  | import org.jboss.ejb3.mdb.ProducerManager;
  | import org.jboss.ejb3.mdb.ProducerObject;
  | import org.jboss.logging.Logger;
  | 
  | @Stateless
  | public class SomeBean implements SomeLocal, SomeRemote {
  | 
  |          //Inject A session contet resource from which the JMS Producer will be created.
  | 	@Resource
  | 	private SessionContext sessionCtx;
  | 
  |         public void callTheAThreadedProcess() {
  | 		ProducerManager manager = null;
  | 		MyProcessor myProcessor = null;
  | 		try {
  | 			myProcessor = (MyProcessor) sessionCtx.lookup(MyProcessor.class.getName());
  | 			// Make the connection to the JMS Queue as producer
  | 			ProducerObject po = (ProducerObject) myProcessor;
  | 			manager = po.getProducerManager();
  | 			manager.connect();
  |                         String mySeriazableParam= "hello";
  | 			myProcessor. processSomething(mySeriazableParam);
  | 		} catch (Exception e) {
  | 			log.error("Some Error Occured", e);
  | 		} finally {
  | 			try {
  | 				manager.close();
  | 			} catch (JMSException e) {
  | 				log.error(e);
  | 			}
  | 		}
  | 	}
  | 


Note that in Jboss 5 queues are not created automatically and need to be defined in: /deply/messaging/destinations-service.xml

  | <mbean code="org.jboss.jms.server.destination.QueueService"
  | 		name="jboss.messaging.destination:service=Queue,name=post_2_analysts_finder"
  | 		xmbean-dd="xmdesc/Queue-xmbean.xml">
  | 		<depends optional-attribute-name="ServerPeer">
  | 			jboss.messaging:service=ServerPeer
  |  </depends>
  | 		<depends>jboss.messaging:service=PostOffice</depends>
  | 		<attribute name="JNDIName">queue/MyTasksProcess </attribute>
  | 		<attribute name="RedeliveryDelay">10000</attribute>
  | 		<attribute name="MaxDeliveryAttempts">3</attribute>
  | 	</mbean>
  | 

Find complete example at:
[url]
http://www.commonj.com/blogs/?p=263
[/url]

View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4263711#4263711

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4263711



More information about the jboss-user mailing list