users@glassfish.java.net

Re: Glassfish and ActiveMQ

From: <glassfish_at_javadesktop.org>
Date: Tue, 12 Jan 2010 13:32:39 PST

This is for a very simple jms example that I got working 2 days ago.. I don't know the diff between jms in glassfishv3 final server AND ActiveMQ. Maybe you could enlighten me on that. I just used glassfish jms messaging.


1. in my jsf bean (web server/front end side)

I had this code. YOu probably could put this in an app client frontend.

    //JMS functionality
    @Resource(mappedName = "jms/ConnectionFactory")
    private ConnectionFactory connectionFactory;
    @Resource(mappedName="jms/Queue")
    private Queue queue;

.....

 public void sendMessage() {

        Connection connection = null;
        Session session = null;
        MessageProducer messageProducer = null;
        TextMessage message = null;
        final int NUM_MSGS = 3;

        try {
            connection = connectionFactory.createConnection();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            messageProducer = session.createProducer(queue);
            message = session.createTextMessage();

            for (int i = 0; i < NUM_MSGS; i++) {
                message.setText("This is message " + (i + 1));
                System.out.println("Sending message: " + message.getText());
                messageProducer.send(message);
            }

            System.out.println("To see if the bean received the messages,");
            System.out.println(
                    " check <install_dir>/domains/domain1/logs/server.log.");
        } catch (JMSException e) {
            System.out.println("Exception occurred: " + e.toString());
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException e) {
                }
            }
        }
    }


2. in my ejb project I had this code: Look at attachment



3. in my glassfishv3 final admin server console I had to create the JMS Resources, connection and queue defined above. This is the missing thing the example code does not tell you.

4. Nothing went in the deployment descriptors. Used entirely annotations. I believe mappedName is not portable but I found no other permutations that worked for me. I don't plan on changing app servers so in this one case I don't care about portability. I care about it everywhere else. By the time I want to change app servers there certainly will be better examples of portable jms code.

Thats it.

mzzz
[Message sent by forum member 'michaelzzz' (msbizy_at_hotmail.com)]

http://forums.java.net/jive/thread.jspa?messageID=380454