추후에 어딘가에 올라가겠지요.

Simple Sender Sample : SimpleSender.java - based on JMS 1.1

package com.jboss.jms.client;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class SimpleSender {
    
    private String destinationName = "/queue/A";

    public void send() throws Exception {
        
        Context ic = null;
        ConnectionFactory cf = null;
        Connection connection = null;

        try {
            //ic = new InitialContext();
            ic = getInitialContext();

            cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
            Queue queue = (Queue) ic.lookup(destinationName);
            System.out.println("Queue " + destinationName + " exists");

            connection = cf.createConnection();
            Session session = connection.createSession(false,
                    Session.AUTO_ACKNOWLEDGE);
            MessageProducer sender = session.createProducer(queue);

            TextMessage message = session.createTextMessage("Hello!");
            sender.send(message);
            System.out.println("The message was successfully sent to the "    + queue.getQueueName() + " queue");

            connection.start();

        } finally {
            if (ic != null) {
                try {
                    ic.close();
                } catch (Exception e) {
                    throw e;
                }
            }

            // ALWAYS close your connection in a finally block to avoid leaks.
            // Closing connection also takes care of closing its related objects
            // e.g. sessions.
            closeConnection(connection);
        }
    }
    
    private Context getInitialContext() {
        try {
            Hashtable<Object, String> env = new Hashtable<Object, String>();
    
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            env.put(Context.PROVIDER_URL, "localhost:1199");
    
            Context ctx = new InitialContext(env);
            return ctx;
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void closeConnection(Connection con) {
        try {
            if (con != null) {
                con.close();
            }
        } catch (JMSException jmse) {
            System.out.println("Could not close connection " + con + " exception was " + jmse);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        new SimpleSender().send();
    }

}


Simple Receiver Sample : SimpleReceiver.java - based on JMS 1.1
package com.jboss.jms.client;

import java.util.Hashtable;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;

public class SimpleReceiver {
   
    private String destinationName = "/queue/A";

    public void send() throws Exception {
       
        Context ic = null;
        ConnectionFactory cf = null;
        Connection connection = null;

        try {
            //ic = new InitialContext();
            ic = getInitialContext();

            cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
            Queue queue = (Queue) ic.lookup(destinationName);
            System.out.println("Queue " + destinationName + " exists");

            connection = cf.createConnection();
            Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
           
            MessageConsumer consumer = session.createConsumer(queue);

            connection.start();

            while( true ){
                TextMessage message = (TextMessage) consumer.receive(2000);
                if( message == null ) break;
                System.out.println("Message From [" + destinationName + "] is [" + message.getText() + "]");
            }
            System.out.println("There is no more message in Queue");
        } finally {
            if (ic != null) {
                try {
                    ic.close();
                } catch (Exception e) {
                    throw e;
                }
            }

            // ALWAYS close your connection in a finally block to avoid leaks.
            // Closing connection also takes care of closing its related objects
            // e.g. sessions.
            closeConnection(connection);
        }
    }
   
    private Context getInitialContext() {
        try {
            Hashtable<Object, String> env = new Hashtable<Object, String>();
   
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
            env.put(Context.PROVIDER_URL, "localhost:1199");
   
            Context ctx = new InitialContext(env);
            return ctx;
        }catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private void closeConnection(Connection con) {
        try {
            if (con != null) {
                con.close();
            }
        } catch (JMSException jmse) {
            System.out.println("Could not close connection " + con + " exception was " + jmse);
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        new SimpleReceiver().send();
    }

}



크리에이티브 커먼즈 라이센스
Creative Commons License
2008/09/29 22:37 2008/09/29 22:37
http://www.javapattern.info/trackback/163
YOUR COMMENT IS THE CRITICAL SUCCESS FACTOR FOR THE QUALITY OF BLOG POST
[로그인][오픈아이디란?]