It's not a secret anymore!
The moose likes EJB and other Java EE Technologies and the fly likes annotations for JNDI lookup Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Reply Bookmark "annotations for JNDI lookup" Watch "annotations for JNDI lookup" New topic
Author

annotations for JNDI lookup

RP kumar
Greenhorn

Joined: Feb 19, 2008
Posts: 1
Hi pls help me to covert the JNDI lookup and initial context of these files into EJB3.0 style using annotaions. It is hardly required for me. Pls do needful ASAP.





//////////////////////////////////////////////
/////////EJB3MDBExample.java//////////////////
//////////////////////////////////////////////

package org.jboss.example.jms.ejb3mdb;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.naming.InitialContext;
import javax.jms.TextMessage;
import javax.jms.Session;
import javax.jms.MessageListener;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.Connection;
import javax.jms.MessageProducer;

@MessageDriven(activationConfig =
{
@ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"),
@ActivationConfigProperty(propertyName="destination", propertyValue="queue/newQueue"),
@ActivationConfigProperty(propertyName="DLQMaxResent", propertyValue="10")
})
public class EJB3MDBExample implements MessageListener
{
public void onMessage(Message m)
{
businessLogic(m);
}

private void businessLogic(Message m)
{
Connection conn = null;
Session session = null;

try
{
TextMessage tm = (TextMessage)m;

String text = tm.getText();
System.out.println("message " + text + " received");


String result = "";
for(int i = 0; i < text.length(); i++)
{
result = text.charAt(i) + result;
}

System.out.println("message processed, result: " + result);


InitialContext ic = new InitialContext();


ConnectionFactory cf = (ConnectionFactory)ic.lookup("java:/JmsXA");
ic.close();

conn = cf.createConnection();
conn.start();
session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);

Destination replyTo = m.getJMSReplyTo();
MessageProducer producer = session.createProducer(replyTo);
TextMessage reply = session.createTextMessage(result);

producer.send(reply);
producer.close();

}
catch(Exception e)
{
e.printStackTrace();
System.out.println("The Message Driven Bean failed!");
}
finally
{
if (conn != null)
{
try
{
conn.close();
}
catch(Exception e)
{
System.out.println("Could not close the connection!" +e);
}
}
}
}
}


//////////////////////////////////////////////
/////////Sender.java//////////////////
//////////////////////////////////////////////

package org.jboss.example.jms.ejb3mdb;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import org.jboss.example.jms.common.ExampleSupport;

public class Sender extends ExampleSupport
{
public void example() throws Exception
{
String destinationName = getDestinationJNDIName();

InitialContext ic = new InitialContext();

ConnectionFactory cf = (ConnectionFactory)ic.lookup("/ConnectionFactory");
Queue queue = (Queue)ic.lookup(destinationName);

log("Queue " + destinationName + " exists");

Connection connection = cf.createConnection();
Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);

Queue temporaryQueue = session.createTemporaryQueue();
MessageConsumer consumer = session.createConsumer(temporaryQueue);

TextMessage message = session.createTextMessage("Hello!");
message.setJMSReplyTo(temporaryQueue);

sender.send(message);

log("The " + message.getText() + " message was successfully sent to the " + queue.getQueueName() + " queue");

connection.start();

message = (TextMessage)consumer.receive(5000);

if (message == null)
{
throw new Exception("Have not received any reply. The example failed!");
}

log("Received message: " + message.getText());

assertEquals("!olleH", message.getText());

displayProviderInfo(connection.getMetaData());

connection.close();
}

protected boolean isQueueExample()
{
return true;
}

public static void main(String[] args)
{
new Sender().run();
}
}
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
Homework: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: annotations for JNDI lookup
 
Similar Threads
MDB onMessage not invoked
MDB deployment on JBoss
which jar files need to run normal JMS application
MDB not recieving the message not throwing any exception
jms queue fetching of message as and when it is posted in asynchronous communication