public
Created

  • Download Gist
DynamicJmsChannels.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
/*
* 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());
 
}
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.