[jboss-user] [EJB 3.0 Users] - Re: How to load external parameters in ejb?

mnenchev do-not-reply at jboss.com
Fri Nov 13 07:36:09 EST 2009


Hi, thanks for the response.
I looked at the tutorials and the code examples, and i did the following:
1) In my ear meta-inf i created jboss.xml :
<?xml version="1.0" encoding="UTF-8"?>
  | 
  | <jboss>
  | 	<enterprise-beans>
  | 		<session>
  | 			<ejb-name>QueueSenderBean</ejb-name>
  | 
  | 			<resource-ref>
  | 				<res-ref-name>CF.1</res-ref-name>
  | 				<jndi-name>CF.1</jndi-name>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<res-ref-name>CF.2</res-ref-name>
  | 				<jndi-name>CF.2</jndi-name>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<res-ref-name>CF.3</res-ref-name>
  | 				<jndi-name>CF.3</jndi-name>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<res-ref-name>QUEUE1</res-ref-name>
  | 				<jndi-name>QUEUE1</jndi-name>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<res-ref-name>QUEUE2</res-ref-name>
  | 				<jndi-name>QUEUE2</jndi-name>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<res-ref-name>QUEUE3</res-ref-name>
  | 				<jndi-name>QUEUE3</jndi-name>
  | 			</resource-ref>
  | 		</session>
  | 	</enterprise-beans>
  | </jboss>

2) After that i created ejb-jar.xml :
<?xml version="1.0" encoding="UTF-8"?>
  | <ejb-jar>
  | 	<enterprise-beans>
  | 		<session>
  |          <ejb-name>QueueSenderBean</ejb-name>
  |          <ejb-class>x.y.x.QueueSenderBean</ejb-class>
  |          <home>x.y.z.IQueueSenderLocal</home>
  |          <session-type>Stateless</session-type>
  |          <transaction-type>Container</transaction-type>
  | 		
  | 			<resource-ref>
  | 				<description>Websphere Connection Factory</description>
  | 				<res-ref-name>CF.1</res-ref-name>
  | 				<res-type>javax.jms.QueueConnectionFactory</res-type>
  | 				<res-auth>Container</res-auth>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<description>Websphere Connection Factory</description>
  | 				<res-ref-name>CF.2</res-ref-name>
  | 				<res-type>javax.jms.QueueConnectionFactory</res-type>
  | 				<res-auth>Container</res-auth>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<description>Websphere Connection Factory</description>
  | 				<res-ref-name>CF.3</res-ref-name>
  | 				<res-type>javax.jms.QueueConnectionFactory</res-type>
  | 				<res-auth>Container</res-auth>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<description>Websphere Queue</description>
  | 				<res-ref-name>Queue1</res-ref-name>
  | 				<res-type>javax.jms.Queue</res-type>
  | 				<res-auth>Container</res-auth>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<description>Websphere Queue</description>
  | 				<res-ref-name>Queue2</res-ref-name>
  | 				<res-type>javax.jms.Queue</res-type>
  | 				<res-auth>Container</res-auth>
  | 			</resource-ref>
  | 
  | 			<resource-ref>
  | 				<description>Websphere Queue</description>
  | 				<res-ref-name>Queue3</res-ref-name>
  | 				<res-type>javax.jms.Queue</res-type>
  | 				<res-auth>Container</res-auth>
  | 			</resource-ref>
  | 		</session>
  | 	</enterprise-beans>
  | </ejb-jar>
  | 

3) Simple send method:
  | // The method is in statless ejb :QueueSenderBean.java
  | public void sendToQueue(Serializable obj, String queueName, String cf) {
  | 		Connection queueConnection = null;
  | 		Session queueSession = null;
  | 		Queue queue = null;
  | 		MessageProducer sender = null;
  | 		try {
  | 			final Context initCtx = new InitialContext();
  | 			final ConnectionFactory qFactory = (ConnectionFactory) initCtx.lookup(queueName);
  | 			queueConnection = qFactory.createConnection();
  | 			queueSession = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  | 			queue = (Queue) initCtx.lookup(cf);
  | 			sender = queueSession.createProducer(queue);
  | 			log.debug("Sending message to " + queue);
  | 			final Message msg = queueSession.createTextMessage((String) obj);
  | 			sender.send(msg);
  | 		} catch (final javax.jms.JMSException e) {
  | 			log.error("send() -> JMS Error: ", e);
  | 		} catch (final NamingException e) {
  | 			log.error("sendToQueue() -> JNI Error: ", e);
  | 		} catch (final Exception e) {
  | 			log.error("sendToQueue() -> Error: ", e);
  | 		} finally {
  | 			try {
  | 				if (queueConnection != null) {
  | 					queueConnection.close();
  | 				}
  | 				if (queueSession != null) {
  | 					queueSession.close();
  | 				}
  | 				queue = null;
  | 				if (sender != null) {
  | 					sender.close();
  | 				}
  | 			} catch (final Exception e) {
  | 				log.error("sendToQueue() ->  Error while closing resources: ", e);
  | 			}
  | 		}
  | 	}

And i call it like this:
sender.sendToQueue(xml, "java:comp/env/QUEUE1", "java:comp/env/CF.1");

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

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



More information about the jboss-user mailing list