Click here to Skip to main content
15,892,674 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 128.1K   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.io.*;
import java.util.*;
import javax.jms.*;
import javax.naming.*;

public class JMSChatPublisher {
  public 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");
    environment.put(Context.SECURITY_PRINCIPAL, principle);
    environment.put(Context.SECURITY_CREDENTIALS, credentials);

    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,
                                                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 TopicPublisher createTopicPublisher(TopicSession session, Topic topic) throws
      JMSException {
    return session.createPublisher(topic);
  }

  private void sendTextMessage(TopicPublisher publisher, TextMessage message) throws
      JMSException {
    publisher.publish(message);
  }

  public static void main(String[] args) {
    String message = "";
    System.out.println("Write Quit to exit");
    JMSChatPublisher client = new JMSChatPublisher();
    InitialContext ctx = null;
    TopicConnectionFactory factory = null;
    Topic topic = null;
    TopicConnection connection = null;
    TopicSession session = null;
    TopicPublisher publisher = 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, principle, credentials);
      session = client.createTopicSession(connection);
      publisher = client.createTopicPublisher(session, topic);
      try {
              BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
              while (message != null) {
                  message = in.readLine();
                  if("quit".equalsIgnoreCase(message))
                    break;
                  TextMessage textMessage = session.createTextMessage(message);
                  client.sendTextMessage(publisher, textMessage);
              }
          } catch (IOException e) {
          }

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

  private final static String topicName = "topic/testTopic";
  private final static String connectionFactory = "HTTPConnectionFactory";
  private final static String principle = "guest";
  private final static String credentials = "guest";

}

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