001/* 002 * Copyright 2002-2012 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.jms.core.support; 018 019import javax.jms.ConnectionFactory; 020 021import org.apache.commons.logging.Log; 022import org.apache.commons.logging.LogFactory; 023 024import org.springframework.beans.factory.BeanInitializationException; 025import org.springframework.beans.factory.InitializingBean; 026import org.springframework.jms.core.JmsTemplate; 027 028/** 029 * Convenient super class for application classes that need JMS access. 030 * 031 * <p>Requires a ConnectionFactory or a JmsTemplate instance to be set. 032 * It will create its own JmsTemplate if a ConnectionFactory is passed in. 033 * A custom JmsTemplate instance can be created for a given ConnectionFactory 034 * through overriding the {@link #createJmsTemplate} method. 035 * 036 * @author Mark Pollack 037 * @since 1.1.1 038 * @see #setConnectionFactory 039 * @see #setJmsTemplate 040 * @see #createJmsTemplate 041 * @see org.springframework.jms.core.JmsTemplate 042 */ 043public abstract class JmsGatewaySupport implements InitializingBean { 044 045 /** Logger available to subclasses */ 046 protected final Log logger = LogFactory.getLog(getClass()); 047 048 private JmsTemplate jmsTemplate; 049 050 051 /** 052 * Set the JMS connection factory to be used by the gateway. 053 * Will automatically create a JmsTemplate for the given ConnectionFactory. 054 * @see #createJmsTemplate 055 * @see #setConnectionFactory(javax.jms.ConnectionFactory) 056 */ 057 public final void setConnectionFactory(ConnectionFactory connectionFactory) { 058 this.jmsTemplate = createJmsTemplate(connectionFactory); 059 } 060 061 /** 062 * Create a JmsTemplate for the given ConnectionFactory. 063 * Only invoked if populating the gateway with a ConnectionFactory reference. 064 * <p>Can be overridden in subclasses to provide a JmsTemplate instance with 065 * a different configuration. 066 * @param connectionFactory the JMS ConnectionFactory to create a JmsTemplate for 067 * @return the new JmsTemplate instance 068 * @see #setConnectionFactory 069 */ 070 protected JmsTemplate createJmsTemplate(ConnectionFactory connectionFactory) { 071 return new JmsTemplate(connectionFactory); 072 } 073 074 /** 075 * Return the JMS ConnectionFactory used by the gateway. 076 */ 077 public final ConnectionFactory getConnectionFactory() { 078 return (this.jmsTemplate != null ? this.jmsTemplate.getConnectionFactory() : null); 079 } 080 081 /** 082 * Set the JmsTemplate for the gateway. 083 * @see #setConnectionFactory(javax.jms.ConnectionFactory) 084 */ 085 public final void setJmsTemplate(JmsTemplate jmsTemplate) { 086 this.jmsTemplate = jmsTemplate; 087 } 088 089 /** 090 * Return the JmsTemplate for the gateway. 091 */ 092 public final JmsTemplate getJmsTemplate() { 093 return this.jmsTemplate; 094 } 095 096 @Override 097 public final void afterPropertiesSet() throws IllegalArgumentException, BeanInitializationException { 098 if (this.jmsTemplate == null) { 099 throw new IllegalArgumentException("'connectionFactory' or 'jmsTemplate' is required"); 100 } 101 try { 102 initGateway(); 103 } 104 catch (Exception ex) { 105 throw new BeanInitializationException("Initialization of JMS gateway failed: " + ex.getMessage(), ex); 106 } 107 } 108 109 /** 110 * Subclasses can override this for custom initialization behavior. 111 * Gets called after population of this instance's bean properties. 112 * @throws java.lang.Exception if initialization fails 113 */ 114 protected void initGateway() throws Exception { 115 } 116 117}