As JB stated, this discussion board was by request from me. I thought it would be neat to have a special place on the forum where us techies can post and discuss different pieces of code and help each other with problems or questions we may have.
With that out of the way, the specification I was most interested in getting some assistance with is Sun's Java Message Service, or, as I will refer to it from now on, JMS. JMS is a Java specification that uses the concept of messages to transfer data from one application to another. The data is stored in the form of a message, and this concept is also used in the Publish/Subscribe format of JMS, where you have Topic Publishers and Topic Subscribers.
I would like to know if anyone here is familiar with JMS, and if so, please state how you learned JMS, and how you currently use it. I am also interested in JMS application design and coding, and would like to see some examples of JMS in action. I don't want anyone to post patented code or anything like that, just simple examples of code that accomplish a certain task in the form of JMS.
For those of you who are new to JMS and would like to learn more about the specification, feel free to visit Sun's website here
Thanks, and I look forward to some contributions from our great forum members!
I thought you could enough this small archive of code snippets. Its a neat little collection that includes snippets of code to delete jms messages and parse them for specific words and phrases.
https://dev2dev.bea.com/jms/code.html
For those of you who are unfamiliar with JMS, here is a great website to help you understand.
https://www.weblogic.com/docs51/intro/intro_jms.html
Here is some simple code to publish a message to a topic for a subsequent subscriber to pick up. This would be used in a pub/sub environment.
CODE |
import javax.jms.*; import javax.naming.*; public class TestTopicPublisher { public static void main(String[] args) { //initialize the topic-related objects Context namingContext = null; TopicConnectionFactory connFactory = null; TopicConnection connection = null; TopicSession session = null; TopicPublisher publisher = null; Topic myTopic = null; TextMessage myMessage = null; System.out.println("Starting up the publisher."); //first, find the JNDI naming service try{ namingContext = new InitialContext(); } catch (NamingException e){ System.out.println ("Could not locate naming service"); e.printStackTrace(); } //use the naming service to find the connection factory and the topic try{ connFactory = (TopicConnectionFactory) namingContext.lookup("TopicConnectionFactory"); myTopic = (Topic)namingContext.lookup("TestTopic"); } catch (NamingException e){ System.out.println ("Could not locate factory or topic"); e.printStackTrace(); } try{ //create the topic connection and topic session connection = connFactory.createTopicConnection(); session = connection.createTopicSession (false, Session.AUTO_ACKNOWLEDGE); //create a topic publisher to send the message publisher = session.createPublisher(myTopic); //create and publish some messages for (int I = 1; I <= 4; I++){ myMessage = session.createTextMessage("Message number " + I); publisher.publish(myMessage); System.out.println("Publishing: " + myMessage.getText()); //wait before sending the next message try { Thread.sleep(5000); } catch (Exception e){} } } catch (JMSException e){ System.out.println("Exception when using message server"); e.printStackTrace(); } finally{ if (connection != null) { try{ connection.close(); } catch (JMSException e) {} } } } } |