[jboss-user] [JBoss Seam] - Right approach using EntityManager and MDB

hispeedsurfer do-not-reply at jboss.com
Fri Nov 9 07:46:33 EST 2007


Hi

I have not much experience with MDBs.

I need to use EntityManager in combination with a MDB. As I have seen here in forum an other user have used this in the same way as follow:

MDB
package de.oats.business.mdb;
  | 
  | import javax.annotation.Resource;
  | import javax.ejb.ActivationConfigProperty;
  | import javax.ejb.MessageDriven;
  | import javax.ejb.MessageDrivenContext;
  | import javax.jms.Message;
  | import javax.jms.MessageListener;
  | import javax.jms.ObjectMessage;
  | 
  | import org.jboss.annotation.ejb.PoolClass;
  | import org.jboss.seam.annotations.In;
  | 
  | 
  | @MessageDriven(name="QueryMessageBean", activationConfig = {
  |     @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
  |     @ActivationConfigProperty(propertyName="destination", propertyValue="queue/oatsMessageQueue")
  | 
  | })
  | @PoolClass(value=org.jboss.ejb3.StrictMaxPool.class, maxSize=1)
  | public class QueryMessageBean implements MessageListener {
  | 
  |     @Resource
  |     private MessageDrivenContext context;
  |     @In(create=true)
  | 	private UserQuery userquery;
  | 
  |     public void onMessage(Message message) {
  |     	QueryTest name = null;
  |     	
  |         try {
  |             if (message instanceof ObjectMessage) {
  |                 ObjectMessage objMessage = (ObjectMessage) message;
  |                 Object obj = objMessage.getObject();
  |                 if (obj instanceof QueryTest) {
  |                     name = (QueryTest) obj;
  |                     userquery.setSqlQuery(name.getSqlQuery());
  |                     userquery.query();
  |                  } else {
  |                     System.err.println("Expecting ProcessDTO in Message");
  |                 }
  |             } else {
  |                 System.err.println("Expecting Object Message");
  |             }
  |         } catch (Throwable t) {
  |             t.printStackTrace();
  |             context.setRollbackOnly();
  |         }
  |     }     
  | }
  | 
  | 

SLSB
package de.oats.business.mdb;
  | 
  | import java.io.Serializable;
  | import java.sql.Connection;
  | import java.sql.ResultSet;
  | import java.sql.Statement;
  | import java.util.Date;
  | 
  | import javax.ejb.Stateless;
  | import javax.persistence.EntityManager;
  | import javax.persistence.PersistenceContext;
  | 
  | import org.jboss.seam.annotations.In;
  | import org.jboss.seam.annotations.Name;
  | 
  | import de.oats.model.QueryResult;
  | 
  | @Stateless
  | @Name("userQuery")
  | public class UserQueryBean implements UserQuery{
  | 	
  | 	private String sqlQuery;
  | 	
  | 	private static Connection con = null;
  | 	@PersistenceContext
  | 	private EntityManager entityManager;
  | 	
  | 	public UserQueryBean() {
  | 		super();
  | 	}
  | 
  | 	public String query(){
  | 		ResultSet rs;
  | 		Statement stmt;
  | 		try {
  | 			con = MysqlCon.getConnection();
  | 			stmt = con.createStatement();
  | 			
  | 			rs = stmt.executeQuery(sqlQuery);
  | 			QueryResult qrslt = null;
  | 			while(rs.next()){
  | 				qrslt = new QueryResult();
  | 				qrslt.setAnzahl(rs.getLong("anzahl"));
  | 				qrslt.setY20gpnum(rs.getString("y20gpnum"));
  | 				qrslt.setLat(rs.getDouble("lat"));
  | 				qrslt.setLon(rs.getDouble("lon"));
  | 				
  | 				entityManager.persist(qrslt);
  | 			}
  | 			entityManager.flush();
  | 		} catch (Exception e) {
  | 			// TODO Auto-generated catch block
  | 			e.printStackTrace();
  | 		}
  | 		return "5 seconds for " +kindOfQuery;
  | 	}
  | 	public void setSqlQuery(String sqlQuery) {
  | 		this.sqlQuery = sqlQuery;
  | 	}
  | 
  | }
  | 

But userquery is always null in spite of @In(create=true)

An other try was to put EntityManager in the ObjectMessage(UserTest) - here is null value on entityManager.

Use Seam 2.0 (seam generatet project) and jboss-4.2.1.GA

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

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



More information about the jboss-user mailing list