• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • 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
 
I've been selected to go to the moon! All thanks to this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic