• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

annotations for JNDI lookup

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
}
 
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Homework: http://faq.javaranch.com/java/HowToAskQuestionsOnJavaRanch
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic