Java tutorial
/* * Copyright (c) 2005-2010, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, * Version 2.0 (the "License"); you may not use this file except * in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.sample.jms; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.log4j.PropertyConfigurator; import javax.jms.*; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import java.util.Properties; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; public class SampleQueueReceiver { public static final String QPID_ICF = "org.wso2.andes.jndi.PropertiesFileInitialContextFactory"; private static final Log log = LogFactory.getLog(SampleQueueReceiver.class); private static final String CF_NAME_PREFIX = "connectionfactory."; private static final String CF_NAME = "qpidConnectionfactory"; private static String CARBON_CLIENT_ID = "carbon"; private static String CARBON_VIRTUAL_HOST_NAME = "carbon"; private static String CARBON_DEFAULT_HOSTNAME = "10.100.4.165"; private static String CARBON_DEFAULT_PORT = "5672"; String userName = "admin"; String password = "admin"; String queueName = "testQueue"; long messageCount = 0; private QueueConnection queueConnection; private QueueSession queueSession; public MessageConsumer registerSubscriber() throws NamingException, JMSException { Properties properties = new Properties(); properties.put(Context.INITIAL_CONTEXT_FACTORY, QPID_ICF); properties.put(CF_NAME_PREFIX + CF_NAME, getTCPConnectionURL(userName, password)); properties.put("queue." + queueName, queueName); InitialContext ctx = new InitialContext(properties); // Lookup connection factory QueueConnectionFactory connFactory = (QueueConnectionFactory) ctx.lookup(CF_NAME); queueConnection = connFactory.createQueueConnection(); queueConnection.start(); queueSession = queueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); Queue queue = (Queue) ctx.lookup(queueName); MessageConsumer consumer = queueSession.createConsumer(queue); return consumer; } public void receiveMessages(MessageConsumer consumer) throws NamingException, JMSException { PropertyConfigurator.configure("log4j.properties"); //Receive all the message while (consumer.receive() != null) { messageCount++; } consumer.close(); queueSession.close(); queueConnection.stop(); queueConnection.close(); } private String getTCPConnectionURL(String username, String password) { // amqp://{username}:{password}@carbon/carbon?brokerlist='tcp://{hostname}:{port}' return new StringBuffer().append("amqp://").append(username).append(":").append(password).append("@") .append(CARBON_CLIENT_ID).append("/").append(CARBON_VIRTUAL_HOST_NAME).append("?brokerlist='tcp://") .append(CARBON_DEFAULT_HOSTNAME).append(":").append(CARBON_DEFAULT_PORT).append("'").toString(); } /* *This method contais runnable thread which is used to calculate message per second. */ public void calculate() { Runnable helloRunnable = new Runnable() { public void run() { System.out.println(messageCount); messageCount = 0; } }; ScheduledExecutorService executor = Executors.newScheduledThreadPool(0); executor.scheduleAtFixedRate(helloRunnable, 1, 1, TimeUnit.SECONDS); } }