Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

annotations for JNDI lookup

 
RP kumar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic