Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / Java / Java SE

Using C#, Java Webservices and JMS together

,
Rate me:
Please Sign up or sign in to vote.
3.80/5 (10 votes)
31 Mar 2006CPOL2 min read 128K   2.4K   57  
Bridging the gap between Java and C#, JMS specific message have been send to C# client using web services.
package testwebservices;


import java.util.*;
import javax.jms.*;
import javax.naming.*;

public class JMSChatSubscriber {
  private InitialContext getInitialContext() throws NamingException {
    Hashtable environment = new Hashtable();

    environment.put(Context.INITIAL_CONTEXT_FACTORY,
                    "org.jboss.naming.HttpNamingContextFactory");
    environment.put(Context.URL_PKG_PREFIXES,
                    "org.jboss.naming:org.jnp.interfaces");
    environment.put(Context.PROVIDER_URL,
                    "http://localhost:8080/invoker/JNDIFactory");

    return new InitialContext(environment);

  }

  private TopicConnectionFactory lookupTopicConnectionFactory(InitialContext
      ctx, String factoryName) throws NamingException {
    return (TopicConnectionFactory) ctx.lookup(factoryName);
  }

  private Topic lookupTopic(InitialContext ctx, String strName) throws
      NamingException {
    return (Topic) ctx.lookup(strName);
  }

  private TopicConnection createTopicConnection(TopicConnectionFactory
                                                connectionFactory) throws
      JMSException {
    return connectionFactory.createTopicConnection();
  }

  private TopicConnection createTopicConnection(TopicConnectionFactory
                                                connectionFactory,
                                                String username,
                                                String password) throws
      JMSException {
    return connectionFactory.createTopicConnection(username, password);
  }

  private TopicSession createTopicSession(TopicConnection connection) throws
      JMSException {
    return connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
  }

  private TopicSubscriber createTopicSubscriber(TopicSession session,
                                                Topic topic) throws
      JMSException {
    return session.createSubscriber(topic);
  }

  private TopicSubscriber createTopicSubscriber(TopicSession session,
                                                String sel, Topic topic) throws
      JMSException {
    return session.createDurableSubscriber(topic, "Durable Chat", sel, false);
  }

  public String getSynchronousMessage() {
    String msgText = " NO MESSAGE ";
    JMSChatSubscriber client = new JMSChatSubscriber();
    InitialContext ctx = null;
    TopicConnectionFactory factory = null;
    Topic topic = null;
    TopicConnection connection = null;
    TopicSession session = null;
    TopicSubscriber subscriber = null;
    try {
      ctx = client.getInitialContext();
      factory = client.lookupTopicConnectionFactory(ctx, connectionFactory);
      topic = client.lookupTopic(ctx, topicName);
    }
    catch (NamingException e) {
      e.printStackTrace();
      System.exit(1);
    }
    try {
      connection = client.createTopicConnection(factory);
      session = client.createTopicSession(connection);
      subscriber = client.createTopicSubscriber(session, topic);
      connection.start();
      Message message = subscriber.receive();
      if (message != null) {
        try {

          if (message instanceof TextMessage) {
            msgText = ( (TextMessage) message).getText();
          }
          else {
            msgText = message.toString();
          }
//          System.out.println(msgText);
          return msgText;
        }
        catch (JMSException jmse) {
          jmse.printStackTrace();
        }

        //return m;
      }

    }
    catch (JMSException e) {
      e.printStackTrace();
    }
    finally {
      if (connection != null) {
        try {
          connection.close();
        }
        catch (JMSException e) {}
      }
    }
    return msgText;
  }

  private final static String topicName = "topic/testTopic";
  private final static String connectionFactory = "HTTPConnectionFactory";
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Written By
Architect Catalisse
Pakistan Pakistan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions