001 /* 002 @license.text@ 003 */ 004 package biz.hammurapi.metrics.j2ee; 005 006 import java.util.List; 007 008 import javax.jms.Connection; 009 import javax.jms.ConnectionFactory; 010 import javax.jms.Destination; 011 import javax.jms.JMSException; 012 import javax.jms.Message; 013 import javax.jms.MessageProducer; 014 import javax.jms.Session; 015 import javax.naming.InitialContext; 016 017 import biz.hammurapi.config.ConfigurationException; 018 import biz.hammurapi.xml.dom.AbstractDomObject; 019 020 021 /** 022 * Use this consumer instead of Topic or Queue consumers with JMS 1.1 compatible providers. 023 * @author Pavel Vlasov 024 * @revision $Revision$ 025 */ 026 public class DestinationSliceConsumer extends AbstractJmsSliceConsumer { 027 Connection connection; 028 Destination destination; 029 030 public void start() throws ConfigurationException { 031 try { 032 InitialContext jndiContext = createContext(); 033 ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup(AbstractDomObject.getElementText(configElement, "connection-factory")); 034 destination = (Destination) jndiContext.lookup(AbstractDomObject.getElementText(configElement, "destination")); 035 String user = AbstractDomObject.getElementText(configElement, "user"); 036 if (user==null) { 037 connection = connectionFactory.createConnection(); 038 } else { 039 connection = connectionFactory.createConnection(user, AbstractDomObject.getElementText(configElement, "password")); 040 } 041 jndiContext.close(); 042 } catch (Exception e) { 043 throw new ConfigurationException(e); 044 } 045 } 046 047 protected boolean processSlices(List slices) { 048 try { 049 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); 050 try { 051 MessageProducer producer = session.createProducer(destination); 052 try { 053 Message message = createMessage(session, slices); 054 producer.send(message); 055 } finally { 056 producer.close(); 057 } 058 } finally { 059 session.close(); 060 } 061 } catch (Exception e) { 062 return false; 063 } 064 return true; 065 } 066 067 public void stop() throws ConfigurationException { 068 try { 069 connection.close(); 070 } catch (JMSException e) { 071 throw new ConfigurationException(); 072 } 073 } 074 075 public void setOwner(Object owner) { 076 // Ignore 077 } 078 }