In this post, we are going to show you on how to send and delete message from a JMS Queue in weblogic.
Here is the code to send the message to the JMS Queue on weblogic server:
package com.togotutor.jms; import java.util.Properties; import java.util.Scanner; import javax.jms.JMSException; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.jms.Session; import javax.jms.TextMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; /** * @author Administrator * */ public class JmsTestApp { private static QueueConnectionFactory JMSConnFactory = null; private static Queue JMSQueue = null; private static QueueConnection JMSQConn = null; private static QueueSession JMSQSession = null; private static QueueSender JMSQSender = null; private static TextMessage JMSTextMsg = null; private static String JNDI_JMSCONNECTION_FACTORY = \“jms/JMSConnectionFactory\”; private static String JNDI_JMSQUEUE = \“jms/togodistQueue\”; private static String URL = \“t3://192.168.1.102:7002\”; private static String USERNAME = \“weblogic\”; private static String PASSWORD = \“weblogic01\”; private static String Message = getUserInput();public static String getUserInput() { Scanner scanner = new Scanner(System.in); System.out.println(\“Type Message Here? then press ENTER\”); String Message = scanner.nextLine(); return Message; }public void SendJMSMessage() throws JMSException, NamingException { //Creating an Initial Context Properties props = new Properties(); /*Alternate Hashtable props = new Hashtable(); */ props.put(Context.INITIAL_CONTEXT_FACTORY, \“weblogic.jndi.WLInitialContextFactory\”); props.put(Context.PROVIDER_URL, URL); props.put(Context.SECURITY_PRINCIPAL, USERNAME); props.put(Context.SECURITY_CREDENTIALS, PASSWORD); InitialContext ctx = new InitialContext(props); try { //Looking up connection Factory Object JMSConnFactory = (QueueConnectionFactory)ctx.lookup(JNDI_JMSCONNECTION_FACTORY); System.out.println(\“Found JMS Connection Factory :\” + JMSConnFactory.toString()); //Variable to Create a Connection JMSQConn = JMSConnFactory.createQueueConnection();System.out.println(\“Checking Connection:\” +JMSQConn);//Looking up JMS Queue Object JMSQueue = (Queue)ctx.lookup(JNDI_JMSQUEUE);System.out.println(\“Found JMS Queue :\” + JMSQueue.toString());//Create a session object JMSQSession = JMSQConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); System.out.println(\“Checking Session:\” +JMSQSession); //Create a Sender or Publisher JMSQSender = JMSQSession.createSender(JMSQueue); //Create a Message Object JMSTextMsg = JMSQSession.createTextMessage(); //Set Text Message JMSTextMsg.setText(Message); System.out.println(JMSTextMsg.getText()); //Send the Message JMSQSender.send(JMSTextMsg); //Close open resources JMSQSender.close(); JMSQConn.close(); JMSQSession.close(); } catch (NamingException e) { e.printStackTrace(); }} public static void main(String[] args) throws JMSException, NamingException { JmsTestApp JTA = new JmsTestApp(); JTA.SendJMSMessage(); }}
Weblogic Console Shows number of current messages in the Queue:
Here is the WLST script, that prints the current message count, deletes the messages from the queue and then prints the current message count to show the message has been deleted.
The Weblogic console view, shows that the current message count is now 0, after the deletion of messages.