Show TOC

Example documentationSender JMS Client Example Locate this document in the navigation structure

 

A sender JMS client can be a remote (standalone) client or a client residing on the server. This is the code relevant for the client, assuming that the client's name is SimpleSenderClient.java:

Syntax Syntax

  1. import java.util.Properties;
    import javax.jms.*;
    import javax.naming.*;
    
    /**
     * The SimpleClient class sends several messages to a queue.
     */
    
    public class SimpleSenderClient {
    
    	private static final String USER = "User"; //Set the "User"
    
    	private static final String PASSWORD = "Password"; //Set the "Password"
    
    	private static final String SAP_NAMING_PROVIDER_URL = "server_host:p4_port"; // Set the <server_host> and the <p4_port>
    
    	private static final String SAP_INITIAL_CONTEXT_FACTORY_IMPL = "com.sap.engine.services.jndi.InitialContextFactoryImpl";
    
    	/**
    	 * Main method.
    	 */
    
    	public static void main(String[] args) {
    		Context jndiContext = null;
    		QueueConnectionFactory queueConnectionFactory = null;
    		QueueConnection queueConnection = null;
    		QueueSession queueSession = null;
    		Queue queue = null;
    		QueueSender queueSender = null;
    		TextMessage message = null;
    		final int NUM_MSGS = 3;
    
    		/* JNDI service naming environment properties initialization */
    
    		Properties ctxProp = new Properties();
    		ctxProp.put(Context.INITIAL_CONTEXT_FACTORY,
    				SAP_INITIAL_CONTEXT_FACTORY_IMPL);
    		ctxProp.put(Context.PROVIDER_URL, SAP_NAMING_PROVIDER_URL);
    		ctxProp.put(Context.SECURITY_PRINCIPAL, USER);
    		ctxProp.put(Context.SECURITY_CREDENTIALS, PASSWORD);
    
    		/*
    		 * Create a JNDI API InitialContext object.
    		 */
    
    		try {
    			jndiContext = new InitialContext(ctxProp);
    		} catch (NamingException e) {
    			System.out.println("Could not create JNDI API" + "context: "
    					+ e.toString());
    			e.printStackTrace();
    			System.exit(1);
    		}
    
    		/*
    		 * Look up connection factory and queue. If either does not exist,   exit. 
    		 * 
    		 */
    
    		try {
    			queueConnectionFactory = (QueueConnectionFactory) jndiContext
    					.lookup("jmsfactory/default/QueueConnectionFactory");
    			System.out.println("QueueConnectionFactory looked up successully");
    			queue = (Queue) jndiContext
    					.lookup("jmsqueues/default/MyVeryNewQueue");
    			System.out.println("MyVeryNewQueue looked up successully");
    		} catch (NamingException e) {
    			System.out.println("JNDI API lookup failed: " + e.toString());
    			e.printStackTrace();
    			System.out.println("Client will exit");
    			System.exit(1);
    		}
    
    		/*
    		 * Create connection. Create session from connection; false means
    		 * session is not transacted. Create sender and text message. Send
    		 * messages, verifying text slightly. Finally, close connection.
    		 */
    
    		try {
    			queueConnection = queueConnectionFactory.createQueueConnection();
    			queueSession = queueConnection.createQueueSession(false,
    					Session.AUTO_ACKNOWLEDGE);
    			queueSender = queueSession.createSender(queue);
    			message = queueSession.createTextMessage();
    			for (int i = 0; i < NUM_MSGS; i++) {
    				message.setText("This is message: " + (i + 1));
    				System.out.println("Sending message: " + message.getText());
    				queueSender.send(message);
    			}
    		} catch (JMSException e) {
    			System.out.println(" JMS Exception occured: " + e.toString());
    			e.printStackTrace();
    		} finally {
    			if (queueConnection != null) {
    				try {
    					queueConnection.close();
    					System.out.println("QueueConnection closed");
    				} catch (JMSException e) {
    					e.printStackTrace();
    				}
    			}
    			System.out.println("SimpleClient finished sending messages");
    			System.exit(0);
    		}
    	}
    }
    
    
End of the code.