[jboss-user] [JBoss Messaging] - Re: Selector Help needed - Possible alternative suggestions

anhminh_tran do-not-reply at jboss.com
Tue Jun 26 13:33:35 EDT 2007


BTW here is the consumer that I made.  It's a modification of the one that came with the example code:



  | public class JMSConsumer {
  | 
  | 	public static void main(String[] args) {
  | 		String jndiDestinationName = "/queue/testQueue";
  | 		InitialContext ic = null;
  | 		Connection connection = null;
  | 		boolean deployed = false;
  | 
  | 		try {
  | 			if (!Util.doesDestinationExist(jndiDestinationName)) {
  | 				System.out.println("Destination " + jndiDestinationName + " does not exist, deploying it");
  | 				Util.deployQueue(jndiDestinationName);
  | 				deployed = true;
  | 			}
  | 
  | 			ic = new InitialContext();
  | 			ic.addToEnvironment("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
  | 			ic.addToEnvironment("java.naming.provider.url", "jnp://10.5.1.241:1099");
  | 			ic.addToEnvironment("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
  | 
  | 			ConnectionFactory cf = (ConnectionFactory) ic.lookup("/ConnectionFactory");
  | 			Queue queue = (Queue) ic.lookup(jndiDestinationName);
  | 
  | 			connection = cf.createConnection();
  | 			Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
  | 			
  | 			MessageConsumer consumer = session.createConsumer(queue);
  | 
  | 			connection.start();
  | 
  | 			System.out.println("Waiting for messages");
  | 			System.out.println("--------------------");
  | 			while (true) {
  | 				TextMessage message = (TextMessage) consumer.receive(2000);
  | 				if (message != null) {		
  | 					System.out.println("message=" + message.getText());
  | 					System.out.println("-------------------------------");
  | 					
  | 					// Acknowledge that we've recieved and processed the message successfully
  | 					message.acknowledge();
  | 					Thread.sleep(15000);
  | 				}
  | 			}
  | 		}
  | 		catch (Exception e) {
  | 			e.printStackTrace();
  | 		}
  | 		finally {
  | 			try {
  | 				if (deployed) {
  | 					Util.undeployQueue(jndiDestinationName);
  | 				}
  | 			}
  | 			catch (Exception e) {
  | 				e.printStackTrace();
  | 			}
  | 
  | 			if (ic != null) {
  | 				try {
  | 					ic.close();
  | 				}
  | 				catch (Exception e) {
  | 					e.printStackTrace();
  | 				}
  | 			}
  | 
  | 			// ALWAYS close your connection in a finally block to avoid leaks
  | 			// Closing connection also takes care of closing its related objects
  | 			// e.g. sessions
  | 			try {
  | 				if (connection != null) {
  | 					connection.close();
  | 				}
  | 
  | 			}
  | 			catch (JMSException jmse) {
  | 				System.err.println("Could not close connection " + connection + " exception was " + jmse);
  | 				jmse.printStackTrace();
  | 			}
  | 		}
  | 	}
  | 
  | }
  | 


It seems that on the call to consumer.receive() it gets a batch of messages no matter if I set the PrefetchSize attribute to 1 or not..

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4057800#4057800

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4057800



More information about the jboss-user mailing list