import javax.jms.*; import java.util.Properties; import javax.naming.*;
/** * An example class that represents a producer to a Topic. */ public class Producer { private static final String USER = "Administrator"; private static final String PASSWORD = "admin_pass"; private static final String SAP_NAMING_PROVIDER_URL = "localhost:50004"; private static final String SAP_INITIAL_CONTEXT_FACTORY_IMPL = "com.sap.engine.services.jndi.InitialContextFactoryImpl";
TopicConnectionFactory topicConnectionFactory = null; TopicConnection topicConnection = null; Topic topic = null; TopicSession topicSession = null; TopicPublisher topicPublisher = null;
/** * Get InitialContext with default values. */ private InitialContext getInitialContext() { try { // set the properties for the InitalContext Properties properties = new Properties(); properties.put( Context.INITIAL_CONTEXT_FACTORY, SAP_INITIAL_CONTEXT_FACTORY_IMPL); properties.put(Context.PROVIDER_URL, SAP_NAMING_PROVIDER_URL); properties.put(Context.SECURITY_PRINCIPAL, USER); properties.put(Context.SECURITY_CREDENTIALS, PASSWORD);
// initalize and return the InitalContext with the specified properties return new InitialContext(properties);
} catch (NamingException ne) { System.out.println("NamingException: " + ne); }
return null; }
/** * Initialize JMS. */ private void initJMS() { try { InitialContext context = getInitialContext();
// look up the TopicConnectionFactory topicConnectionFactory = (TopicConnectionFactory) context.lookup( " jmsfactory/default/TopicConnectionFactory");
// create topic connection topicConnection = topicConnectionFactory.createTopicConnection();
// start the connection topicConnection.start();
} catch (NamingException ne) { System.out.println("NamingException : " + ne); } catch (JMSException jmse) { System.out.println("JMSException : " + jmse); } }
/** * Closes all resorces used in this test. This should be called * when you want to finish using JMS. */ private void closeJMS() { try {
//close the jms topic session topicSession.close();
//close the jms topic connection topicConnection.close();
} catch (JMSException jmse) { System.out.println("JMSException: " + jmse); } }
/** * Create a topic connection and a session. * Then use the session to create a topic publisher. */ public void aMethod() { try {
// initializes all important JMS data initJMS();
// create topic session topicSession = topicConnection.createTopicSession(false, 1);
// create topic destination topic = topicSession.createTopic("ExampleTopic");
// create topic publisher topicPublisher = topicSession.createPublisher(topic);
/** * Publish some messages. */
// Closes all resources used in this test. closeJMS();
} catch (JMSException jmse) { System.out.println("JMSException : " + jmse); } }
} |