config.xml
... |
jndi.properties
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory java.naming.provider.url=t3://localhost:7001 |
JMSTopicPublisher.java
package dk.topsecurity; import javax.jms.*; import javax.naming.*; import java.text.SimpleDateFormat; /** * The publish/subscribe (pub/sub) messaging model enables an application to * send a message to multiple applications. Pub/sub messaging applications * send and receive messages by subscribing to a topic. A topic publisher * (producer) sends messages to a specific topic. A topic subscriber * (consumer) retrieves messages from a specific topic. */ public class JMSTopicPublisher { public final static String TOPIC_NAME = "dk.topsecurity.topic"; public final static String JMS_FACTORY = "weblogic.jms.ConnectionFactory"; // public final static String JMS_FACTORY = "javax.jms.TopicConnectionFactory" public static void main(String[] args) { try { //requires definition of in java.naming.factory.initial,java.naming.provider.url in jndi.properties Context ctx = new InitialContext(); TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); //get Topic Connection Factory by JNDI TopicConnection conn = factory.createTopicConnection(); //create a new Topic Connection TopicSession session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //create a non-transactional TopicSession Topic topic = null; try { topic = (Topic) ctx.lookup(TOPIC_NAME); //check if someone has already created the topic } catch (NameNotFoundException exc) { topic = session.createTopic(TOPIC_NAME); ctx.bind(TOPIC_NAME, topic); //if not, create a new topic and store it in JNDI directory } TopicPublisher sender = session.createPublisher(topic); //create a publisher to publish msg conn.start(); //tell Topic Connection you are ready to interact with the message service for (;;) { String messageTxt = "Hello from publisher at " + (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new java.util.Date()); TextMessage message = session.createTextMessage(messageTxt); //create simple text message sender.publish(message); //publish the message try { Thread.sleep(5000); } catch (Exception ignore) { } //5 secs pause before another message } } catch (Exception exc) { exc.printStackTrace(); } } } |
JMSTopicReceiver.java
package dk.topsecurity; import javax.jms.*; import javax.naming.*; /** * The publish/subscribe (pub/sub) messaging model enables an application to * send a message to multiple applications. Pub/sub messaging applications * send and receive messages by subscribing to a topic. A topic publisher * (producer) sends messages to a specific topic. A topic subscriber * (consumer) retrieves messages from a specific topic. */ public class JMSTopicReceiver { public final static String TOPIC_NAME = "dk.topsecurity.topic"; public final static String JMS_FACTORY = "weblogic.jms.ConnectionFactory"; // public final static String JMS_FACTORY = "javax.jms.TopicConnectionFactory" public static void main(String[] args) { try { //requires definition of in java.naming.factory.initial,java.naming.provider.url in jndi.properties Context ctx = new InitialContext(); TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); //get Topic Connection Factory by JNDI TopicConnection conn = factory.createTopicConnection(); //create new Topic Connection TopicSession session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); //create non-transactional Topic Session Topic topic = null; try { topic = (Topic) ctx.lookup(TOPIC_NAME); //check if someone has already created the topic } catch (NameNotFoundException exc) { topic = session.createTopic(TOPIC_NAME); ctx.bind(TOPIC_NAME, topic); //if not, create new topic and store it in JNDI directory } TopicSubscriber subscriber = session.createSubscriber(topic); //create subscriber to receive msg conn.start(); //tell the Topic Connection you are ready to interact with the message service for (;;) { //receive next message and echo message contents to console System.out.println(((TextMessage) subscriber.receive()).getText()); //echo message text to console } } catch (Exception exc) { exc.printStackTrace(); } } } |
JMSTopicListener.java
package dk.topsecurity; import javax.jms.*; import javax.naming.*; /** * The publish/subscribe (pub/sub) messaging model enables an application to * send a message to multiple applications. Pub/sub messaging applications * send and receive messages by subscribing to a topic. A topic publisher * (producer) sends messages to a specific topic. A topic subscriber * (consumer) retrieves messages from a specific topic. */ public class JMSTopicListener implements MessageListener { public final static String TOPIC_NAME = "dk.topsecurity.topic"; public final static String JMS_FACTORY = "weblogic.jms.ConnectionFactory"; // public final static String JMS_FACTORY = "javax.jms.TopicConnectionFactory" JMSTopicListener() { } public void onMessage(Message message) { try { //assumes TextMessage System.out.println(((TextMessage) message).getText()); } catch (Exception exc) { exc.printStackTrace(); } } public static void main(String[] args) { try { //requires definition of in java.naming.factory.initial,java.naming.provider.url in jndi.properties Context ctx = new InitialContext(); TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup(JMS_FACTORY); TopicConnection conn = factory.createTopicConnection(); TopicSession session = conn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = null; try { topic = (Topic) ctx.lookup(TOPIC_NAME); } catch (NameNotFoundException exc) { topic = session.createTopic(TOPIC_NAME); ctx.bind(TOPIC_NAME, topic); } TopicSubscriber subscriber = session.createSubscriber(topic); conn.start(); subscriber.setMessageListener(new JMSTopicListener()); Thread.sleep(999999999); } catch (Exception exc) { exc.printStackTrace(); } } } |
build.xml
<project name="TopsecurityJMS" default="all" basedir="."> <target name="init"> <!-- set global properties for this build --> <property environment="env"/> <property name="wl_home" value="C:/weblogicplatform/weblogic81"/> <property name="source" value="./src/dk/topsecurity"/> <property name="destination" value="./classes"/> <property name="project_name" value="TopsecurityJMS"/> <property name="jmsjar" value="${project_name}.jar"/> </target> <target name="all" depends="makejar"/> <target name="clean" depends="init"> <delete file="*.class"/> </target> <target name="compile" depends="clean"> <mkdir dir="${destination}" /> <javac srcdir="${source}" classpath="${wl_home}\server\lib\weblogic.jar" includes="*.java" destdir="${destination}"/> </target> <!-- create .jar --> <target name="makejar" depends="compile"> <jar jarfile="${jmsjar}" basedir="${destination}" includes="dk/**"> <fileset dir="${destination}"> <include name="dk/**"/> </fileset> </jar> </target> </project> |
topicPublishTest.cmd
set BEA_HOME=C:\weblogicplatform set WL_HOME=C:\weblogicplatform\weblogic81 java -classpath .;TopsecurityJMS.jar;%WL_HOME%\server\lib\weblogic.jar dk/topsecurity/JMSTopicPublisher |
topicReceiveTest.cmd
set BEA_HOME=C:\weblogicplatform set WL_HOME=C:\weblogicplatform\weblogic81 java -classpath .;TopsecurityJMS.jar;%WL_HOME%\server\lib\weblogic.jar dk/topsecurity/JMSTopicReceiver |
topicListenTest.cmd
set BEA_HOME=C:\weblogicplatform set WL_HOME=C:\weblogicplatform\weblogic81 java -classpath .;TopsecurityJMS.jar;%WL_HOME%\server\lib\weblogic.jar dk/topsecurity/JMSTopicListener |