Create the jmsuser user for accessing the AQ
sqlplus SYS/manager1@ORCL AS SYSDBA
grant connect,resource,aq_administrator_role to jmsuser identified by jmsuser;
grant select on sys.DBA_PENDING_TRANSACTIONS to jmsuser;
grant execute on sys.dbms_aqadm to jmsuser;
grant execute on sys.dbms_aq to jmsuser;
grant execute on sys.dbms_aqin to jmsuser;
grant execute on sys.dbms_aqjms to jmsuser;
exec dbms_aqadm.grant_system_privilege('ENQUEUE_ANY','jmsuser');
exec dbms_aqadm.grant_system_privilege('DEQUEUE_ANY','jmsuser');
package webserv1;
import java.util.Hashtable;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class SendMsgQueue {
public SendMsgQueue() {
}
public static void main(String argv[]) throws Exception {
Hashtable env = new Hashtable();
// Standalone OC4J connection details
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "weblogic");
env.put(Context.SECURITY_CREDENTIALS, "welcome1");
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
// env.put(Context.PROVIDER_URL, "t3://ceiacb5.us.oracle.com:7001");
InitialContext context = new InitialContext(env);
// The producer and consumer need to get a connection factory and use it to set up
// a connection and a session
QueueConnectionFactory connFactory = (QueueConnectionFactory) context.lookup("AQJMS_XAConnectionFactory");
QueueConnection conn = connFactory.createQueueConnection();
// This session is not transacted, and it uses automatic message acknowledgement
QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue) context.lookup("jms/JMSDEMO_QUEUE1");
// Sender
QueueSender sender = session.createSender(q);
// Text message
TextMessage msg = session.createTextMessage();
msg.setText("Hello Test123");
System.out.println("Sending the message: "+msg.getText());
sender.send(msg);
session.close();
conn.close();
}
}
Program for Receving the Message
package webserv1;
import java.util.Hashtable;
import javax.jms.Message;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueReceiver;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
public class RecvMsgQueue {
public RecvMsgQueue() {
super();
}
public static void main(String argv[]) throws Exception {
Hashtable env = new Hashtable();
// Standalone OC4J connection details
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.SECURITY_PRINCIPAL, "weblogic");
env.put(Context.SECURITY_CREDENTIALS, "welcome1");
env.put(Context.PROVIDER_URL, "t3://localhost:7001");
// env.put(Context.PROVIDER_URL, "t3://ceiacb5.us.oracle.com:7001");
InitialContext context = new InitialContext(env);
// The producer and consumer need to get a connection factory and use it to set up
// a connection and a session
// QueueConnectionFactory connFactory = (QueueConnectionFactory) context.lookup("jms/oc4jQueueConnFactory");
QueueConnectionFactory connFactory =(QueueConnectionFactory) context.lookup("AQJMS_XAConnectionFactory");
QueueConnection conn = connFactory.createQueueConnection();
// This session is not transacted, and it uses automatic message acknowledgement
QueueSession session = conn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = (Queue) context.lookup("jms/JMSDEMO_QUEUE1");
// Queue q = (Queue) context.lookup("jms/oc4jQueue");
/* // Sender
QueueSender sender = session.createSender(q);
// Text message
TextMessage msg = session.createTextMessage();
msg.setText("Hello there!");
System.out.println("Sending the message: "+msg.getText());
sender.send(msg);
*/
// Receiver
QueueReceiver receiver = session.createReceiver(q);
conn.start();
Message m = receiver.receive();
if(m instanceof TextMessage) {
TextMessage txt = (TextMessage) m;
System.out.println("Message Received: "+txt.getText());
}
session.close();
conn.close();
}
}
No comments:
Post a Comment