4/14/09


Weblogic JMS Queues

Receiving Messages Synchronously:

import javax.servlet.http.HttpSession;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.annotations.Jpf;
import org.apache.beehive.netui.pageflow.Forward;
import org.apache.log.format.Formatter;
import org.apache.log.output.jms.TextMessageBuilder;

import javax.jms.*;
import javax.mail.search.MessageIDTerm;
import javax.naming.*;
import weblogic.jndi.*;
import javax.transaction.UserTransaction;
import weblogic.jms.extensions.*;


@Jpf.Controller(simpleActions = { @Jpf.SimpleAction(name = "begin", path = "index.jsp") })
public class Controller extends PageFlowController {

@Jpf.Action(forwards = { @Jpf.Forward(name = "success", path = "index.jsp") })
public Forward newAction1() throws NamingException, JMSException {

InitialContext ic = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ic.lookup("weblogic.jms.ConnectionFactory");

QueueConnection qc = queueConnectionFactory.createQueueConnection();
QueueSession qSession = qc.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

Queue queue = (Queue) ic.lookup("MyDefaultQueue");

QueueReceiver qReceiver = qSession.createReceiver(queue);
qReceiver = qSession.createReceiver(queue);
//qReceiver.setMessageListener(this);

//QueueSender qSender = qSession.createSender(queue);
//Message msg = qSession.createMessage();
//msg.setStringProperty("msg", "My AA String Message.");
//qSender.send(msg);

qc.start();
System.out.println(qReceiver.receive().getStringProperty("msg"));
qc.close();

Forward forward = new Forward("success");
return forward;
}


@Override
protected void onCreate() {
}

@Override
protected void onDestroy(HttpSession session) {
}
}


--------------------------------------------------------

Receiving Messages Asynchronously:

import javax.servlet.http.HttpSession;
import org.apache.beehive.netui.pageflow.PageFlowController;
import org.apache.beehive.netui.pageflow.annotations.Jpf;
import org.apache.beehive.netui.pageflow.Forward;
import org.apache.log.format.Formatter;
import org.apache.log.output.jms.TextMessageBuilder;

import javax.jms.*;
import javax.mail.search.MessageIDTerm;
import javax.naming.*;
import weblogic.jndi.*;
import javax.transaction.UserTransaction;
import weblogic.jms.extensions.*;


@Jpf.Controller(simpleActions = { @Jpf.SimpleAction(name = "begin", path = "index.jsp") })
public class Controller extends PageFlowController implements javax.jms.MessageListener{

public void onMessage(Message message) {
try {
System.out.println("---------->> " + message.getStringProperty("msg"));
} catch (JMSException e) {
e.printStackTrace();
}
}

@Jpf.Action(forwards = { @Jpf.Forward(name = "success", path = "index.jsp") })
public Forward newAction1() throws NamingException, JMSException {

InitialContext ic = new InitialContext();
QueueConnectionFactory queueConnectionFactory = (QueueConnectionFactory) ic.lookup("weblogic.jms.ConnectionFactory");

QueueConnection qc = queueConnectionFactory.createQueueConnection();
QueueSession qSession = qc.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);

Queue queue = (Queue) ic.lookup("MyDefaultQueue");

QueueReceiver qReceiver = qSession.createReceiver(queue);
qReceiver = qSession.createReceiver(queue);
qReceiver.setMessageListener(this);
qc.start();

QueueSender qSender = qSession.createSender(queue);
Message msg = qSession.createMessage();
msg.setStringProperty("msg", "My BB String Message.");
qSender.send(msg);

Forward forward = new Forward("success");
return forward;
}


@Override
protected void onCreate() {
}

@Override
protected void onDestroy(HttpSession session) {
}
}

No comments: