Created
April 23, 2013 14:05
-
-
Save garyrussell/5443817 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2002-2013 the original author or authors. | |
* | |
* Licensed 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.springframework.integration.samples.jms; | |
import javax.jms.Connection; | |
import javax.jms.ConnectionFactory; | |
import javax.jms.Destination; | |
import javax.jms.MessageProducer; | |
import javax.jms.Session; | |
import org.mockito.Mockito; | |
import org.springframework.beans.BeansException; | |
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; | |
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | |
import org.springframework.beans.factory.support.BeanDefinitionBuilder; | |
import org.springframework.beans.factory.support.BeanDefinitionRegistry; | |
import org.springframework.context.ConfigurableApplicationContext; | |
import org.springframework.context.support.GenericApplicationContext; | |
import org.springframework.integration.core.PollableChannel; | |
import org.springframework.integration.jms.PollableJmsChannel; | |
import org.springframework.integration.message.GenericMessage; | |
import org.springframework.integration.monitor.IntegrationMBeanExporter; | |
import org.springframework.jms.core.JmsTemplate; | |
/** | |
* @author Gary Russell | |
* @since 3.0 | |
* | |
*/ | |
public class DynamicJmsChannels { | |
public static void main(String[] args) throws Exception { | |
// Set up mocks | |
ConnectionFactory connectionFactory = Mockito.mock(ConnectionFactory.class); | |
Connection connection = Mockito.mock(Connection.class); | |
Mockito.when(connectionFactory.createConnection()).thenReturn(connection); | |
Session session = Mockito.mock(Session.class); | |
Mockito.when(connection.createSession(Mockito.anyBoolean(), Mockito.anyInt())).thenReturn(session); | |
MessageProducer producer = Mockito.mock(MessageProducer.class); | |
Mockito.when(session.createProducer(Mockito.any(Destination.class))).thenReturn(producer); | |
JmsTemplate template = new JmsTemplate(connectionFactory); | |
template.setDefaultDestinationName("fooDest"); | |
ConfigurableApplicationContext context = new GenericApplicationContext(); | |
context.addBeanFactoryPostProcessor(new DynamicJmsJmxChannelAdder("foo", template)); | |
context.refresh(); | |
PollableChannel channel = context.getBean(PollableChannel.class); // is the proxy | |
channel.send(new GenericMessage<String>("foo")); | |
// set a breakpoint after the above line and you can see the count increased in the MBean stats | |
// Add another channel - step down to the refresh and see it gets registered. | |
template = new JmsTemplate(connectionFactory); | |
template.setDefaultDestinationName("barDest"); | |
context = new GenericApplicationContext(); | |
context.addBeanFactoryPostProcessor(new DynamicJmsJmxChannelAdder("bar", template)); | |
context.refresh(); | |
channel = context.getBean(PollableChannel.class); // is the proxy | |
} | |
public static final class DynamicJmsJmxChannelAdder implements BeanFactoryPostProcessor { | |
private final String channelName; | |
private final JmsTemplate template; | |
private DynamicJmsJmxChannelAdder(String channelName, JmsTemplate template) { | |
this.channelName = channelName; | |
this.template = template; | |
} | |
@Override | |
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { | |
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(IntegrationMBeanExporter.class); | |
builder.addPropertyValue("defaultDomain", "foo"); | |
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition("exporter", builder.getBeanDefinition()); | |
builder = BeanDefinitionBuilder.rootBeanDefinition(PollableJmsChannel.class); | |
builder.addConstructorArgValue(this.template); | |
((BeanDefinitionRegistry) beanFactory).registerBeanDefinition(this.channelName, builder.getBeanDefinition()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment