users@glassfish.java.net

Re: JMS load balancing does not occur when using message selectors

From: <glassfish_at_javadesktop.org>
Date: Tue, 15 May 2007 04:17:47 PDT

standalone app-client application (launched using GF java web start capabilities).

The client code (except the Swing interface):

package jmsmultipleconsumers;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import javax.annotation.Resource;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.jms.TextMessage;

public class Main {
    
    @Resource(mappedName="jms/CFMultipleConsumers")
    private static ConnectionFactory connectionFactory;
    
    @Resource(mappedName="jms/QMultipleConsumers")
    private static Queue q;
    
    private static int count = 10;
    
    public Main() {
    }
    
    static public void enviarMensaje(String texto) throws Exception{
        Connection connection = connectionFactory.createConnection();
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer prod = session.createProducer(q);
        TextMessage message = session.createTextMessage();
        message.setIntProperty("contador",count);
        connection.start();
        message.setText(texto+(count--));
        prod.send(message);
        connection.close();
        
        
        
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) throws Exception {
        
        //inicia JMS
        Connection connection = connectionFactory.createConnection();
        Session session1 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        Session session2 = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        
        final MessageConsumer cons1 = session1.createConsumer(q, "contador > 0");
        final MessageConsumer cons2 = session2.createConsumer(q, "contador > 3");
        
        final NewJFrame f = new NewJFrame();
        int NTHREADS = 100;
        Executor executor = Executors.newFixedThreadPool(NTHREADS);
        executor.execute(new Runnable() {
            public void run() {
                while(true){
                    try{
                        TextMessage m = (TextMessage) cons1.receive();
                        f.setjTA1(m.getText());
                    }catch(Exception e){
                        e.printStackTrace();
                        break;
                    }
                }
            }
        });
        
        
        executor.execute(new Runnable() {
            public void run() {
                while(true){
                    try{
                        TextMessage m = (TextMessage) cons2.receive();
                        f.setjTA2(m.getText());
                    }catch(Exception e){
                        e.printStackTrace();
                        break;
                    }
                }
            }
        });
        
        //muestra GUI
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                f.setVisible(true);
            }
        });
        
        connection.start();
        
    }
    
    
}
[Message sent by forum member 'andresgr' (andresgr)]

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