dest.setId(source.getId()); dest.setDestination(source.getDestination()); dest.setSubscription(source.getSubscription()); dest.setSelector(source.getSelector()); dest.setConcurrency(source.getConcurrency()); dest.setBean(source.getBean()); dest.setMethod(source.getMethod()); dest.setMostSpecificMethod(source.getMostSpecificMethod()); dest.setMessageHandlerMethodFactory( get(source, this.messageHandlerMethodFactoryField)); dest.setEmbeddedValueResolver(get(source, this.embeddedValueResolverField));
private MessagingMessageListenerAdapter createInstance( DefaultMessageHandlerMethodFactory factory, Method method, MessageListenerContainer container) { MethodJmsListenerEndpoint endpoint = new MethodJmsListenerEndpoint(); endpoint.setBean(this.sample); endpoint.setMethod(method); endpoint.setMessageHandlerMethodFactory(factory); return endpoint.createMessageListener(container); }
/** * Return the default response destination, if any. */ @Nullable protected String getDefaultResponseDestination() { Method specificMethod = getMostSpecificMethod(); if (specificMethod == null) { return null; } SendTo ann = getSendTo(specificMethod); if (ann != null) { Object[] destinations = ann.value(); if (destinations.length != 1) { throw new IllegalStateException("Invalid @" + SendTo.class.getSimpleName() + " annotation on '" + specificMethod + "' one destination must be set (got " + Arrays.toString(destinations) + ")"); } return resolve((String) destinations[0]); } return null; }
@Nullable public Method getMostSpecificMethod() { if (this.mostSpecificMethod != null) { return this.mostSpecificMethod; } Method method = getMethod(); if (method != null) { Object bean = getBean(); if (AopUtils.isAopProxy(bean)) { Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean); method = AopUtils.getMostSpecificMethod(method, targetClass); } } return method; }
endpoint.setBean(bean); endpoint.setMethod(invocableMethod); endpoint.setMostSpecificMethod(mostSpecificMethod); endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory); endpoint.setEmbeddedValueResolver(this.embeddedValueResolver); endpoint.setBeanFactory(this.beanFactory); endpoint.setId(getEndpointId(jmsListener)); endpoint.setDestination(resolve(jmsListener.destination())); if (StringUtils.hasText(jmsListener.selector())) { endpoint.setSelector(resolve(jmsListener.selector())); endpoint.setSubscription(resolve(jmsListener.subscription())); endpoint.setConcurrency(resolve(jmsListener.concurrency()));
@Test public void sendToAnnotationFoundOnCglibProxy() throws Exception { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, ProxyConfig.class, ClassProxyTestBean.class); try { JmsListenerContainerTestFactory factory = context.getBean(JmsListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); JmsListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); assertEquals("Wrong endpoint type", MethodJmsListenerEndpoint.class, endpoint.getClass()); MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint; assertTrue(AopUtils.isCglibProxy(methodEndpoint.getBean())); assertTrue(methodEndpoint.getBean() instanceof ClassProxyTestBean); assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMethod()); assertEquals(ClassProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMostSpecificMethod()); Method method = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination"); ReflectionUtils.makeAccessible(method); Object destination = ReflectionUtils.invokeMethod(method, endpoint); assertEquals("SendTo annotation not found on proxy", "foobar", destination); } finally { context.close(); } }
@Test public void simpleMessageListener() throws Exception { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, SimpleMessageListenerTestBean.class); JmsListenerContainerTestFactory factory = context.getBean(JmsListenerContainerTestFactory.class); assertEquals("One container should have been registered", 1, factory.getListenerContainers().size()); MessageListenerTestContainer container = factory.getListenerContainers().get(0); JmsListenerEndpoint endpoint = container.getEndpoint(); assertEquals("Wrong endpoint type", MethodJmsListenerEndpoint.class, endpoint.getClass()); MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint; assertEquals(SimpleMessageListenerTestBean.class, methodEndpoint.getBean().getClass()); assertEquals(SimpleMessageListenerTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMethod()); assertEquals(SimpleMessageListenerTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMostSpecificMethod()); SimpleMessageListenerContainer listenerContainer = new SimpleMessageListenerContainer(); methodEndpoint.setupListenerContainer(listenerContainer); assertNotNull(listenerContainer.getMessageListener()); assertTrue("Should have been started " + container, container.isStarted()); context.close(); // Close and stop the listeners assertTrue("Should have been stopped " + container, container.isStopped()); }
Assert.state(this.messageHandlerMethodFactory != null, "Could not create message listener - MessageHandlerMethodFactory not set"); MessagingMessageListenerAdapter messageListener = createMessageListenerInstance(); InvocableHandlerMethod invocableHandlerMethod = this.messageHandlerMethodFactory.createInvocableHandlerMethod(getBean(), getMethod()); messageListener.setHandlerMethod(invocableHandlerMethod); String responseDestination = getDefaultResponseDestination(); if (StringUtils.hasText(responseDestination)) { if (container.isReplyPubSubDomain()) {
/** * Instantiate an empty {@link MethodJmsListenerEndpoint} for further * configuration with provided parameters in {@link #processJmsListener}. * @return a new {@code MethodJmsListenerEndpoint} or subclass thereof * @since 4.1.9 * @see MethodJmsListenerEndpoint#createMessageListenerInstance() */ protected MethodJmsListenerEndpoint createMethodJmsListenerEndpoint() { return new MethodJmsListenerEndpoint(); }
endpoint.setBean(bean); endpoint.setMethod(invocableMethod); endpoint.setMostSpecificMethod(mostSpecificMethod); endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory); endpoint.setEmbeddedValueResolver(this.embeddedValueResolver); endpoint.setBeanFactory(this.beanFactory); endpoint.setId(getEndpointId(jmsListener)); endpoint.setDestination(resolve(jmsListener.destination())); if (StringUtils.hasText(jmsListener.selector())) { endpoint.setSelector(resolve(jmsListener.selector())); endpoint.setSubscription(resolve(jmsListener.subscription())); endpoint.setConcurrency(resolve(jmsListener.concurrency()));
@Test public void sendToAnnotationFoundOnInterfaceProxy() throws Exception { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, ProxyConfig.class, InterfaceProxyTestBean.class); try { JmsListenerContainerTestFactory factory = context.getBean(JmsListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); JmsListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); assertEquals("Wrong endpoint type", MethodJmsListenerEndpoint.class, endpoint.getClass()); MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint; assertTrue(AopUtils.isJdkDynamicProxy(methodEndpoint.getBean())); assertTrue(methodEndpoint.getBean() instanceof SimpleService); assertEquals(SimpleService.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMethod()); assertEquals(InterfaceProxyTestBean.class.getMethod("handleIt", String.class, String.class), methodEndpoint.getMostSpecificMethod()); Method method = ReflectionUtils.findMethod(endpoint.getClass(), "getDefaultResponseDestination"); ReflectionUtils.makeAccessible(method); Object destination = ReflectionUtils.invokeMethod(method, endpoint); assertEquals("SendTo annotation not found on proxy", "foobar", destination); } finally { context.close(); } }
Assert.state(this.messageHandlerMethodFactory != null, "Could not create message listener - MessageHandlerMethodFactory not set"); MessagingMessageListenerAdapter messageListener = createMessageListenerInstance(); InvocableHandlerMethod invocableHandlerMethod = this.messageHandlerMethodFactory.createInvocableHandlerMethod(getBean(), getMethod()); messageListener.setHandlerMethod(invocableHandlerMethod); String responseDestination = getDefaultResponseDestination(); if (StringUtils.hasText(responseDestination)) { if (container.isReplyPubSubDomain()) {
@Nullable public Method getMostSpecificMethod() { if (this.mostSpecificMethod != null) { return this.mostSpecificMethod; } Method method = getMethod(); if (method != null) { Object bean = getBean(); if (AopUtils.isAopProxy(bean)) { Class<?> targetClass = AopProxyUtils.ultimateTargetClass(bean); method = AopUtils.getMostSpecificMethod(method, targetClass); } } return method; }
/** * Return the default response destination, if any. */ @Nullable protected String getDefaultResponseDestination() { Method specificMethod = getMostSpecificMethod(); if (specificMethod == null) { return null; } SendTo ann = getSendTo(specificMethod); if (ann != null) { Object[] destinations = ann.value(); if (destinations.length != 1) { throw new IllegalStateException("Invalid @" + SendTo.class.getSimpleName() + " annotation on '" + specificMethod + "' one destination must be set (got " + Arrays.toString(destinations) + ")"); } return resolve((String) destinations[0]); } return null; }
/** * Instantiate an empty {@link MethodJmsListenerEndpoint} for further * configuration with provided parameters in {@link #processJmsListener}. * @return a new {@code MethodJmsListenerEndpoint} or subclass thereof * @since 4.1.9 * @see MethodJmsListenerEndpoint#createMessageListenerInstance() */ protected MethodJmsListenerEndpoint createMethodJmsListenerEndpoint() { return new MethodJmsListenerEndpoint(); }
dest.setId(source.getId()); dest.setDestination(source.getDestination()); dest.setSubscription(source.getSubscription()); dest.setSelector(source.getSelector()); dest.setConcurrency(source.getConcurrency()); dest.setBean(source.getBean()); dest.setMethod(source.getMethod()); dest.setMostSpecificMethod(source.getMostSpecificMethod()); dest.setMessageHandlerMethodFactory( get(source, this.messageHandlerMethodFactoryField)); dest.setEmbeddedValueResolver(get(source, this.embeddedValueResolverField));
endpoint.setBean(bean); endpoint.setMethod(invocableMethod); endpoint.setMostSpecificMethod(mostSpecificMethod); endpoint.setMessageHandlerMethodFactory(this.messageHandlerMethodFactory); endpoint.setEmbeddedValueResolver(this.embeddedValueResolver); endpoint.setBeanFactory(this.beanFactory); endpoint.setId(getEndpointId(jmsListener)); endpoint.setDestination(resolve(jmsListener.destination())); if (StringUtils.hasText(jmsListener.selector())) { endpoint.setSelector(resolve(jmsListener.selector())); endpoint.setSubscription(resolve(jmsListener.subscription())); endpoint.setConcurrency(resolve(jmsListener.concurrency()));
private MethodJmsListenerEndpoint createMethodJmsEndpoint(DefaultMessageHandlerMethodFactory factory, Method method) { MethodJmsListenerEndpoint endpoint = new MethodJmsListenerEndpoint(); endpoint.setBean(listener); endpoint.setMethod(method); endpoint.setMessageHandlerMethodFactory(factory); return endpoint; }
@Test public void metaAnnotationIsDiscovered() throws Exception { ConfigurableApplicationContext context = new AnnotationConfigApplicationContext( Config.class, MetaAnnotationTestBean.class); try { JmsListenerContainerTestFactory factory = context.getBean(JmsListenerContainerTestFactory.class); assertEquals("one container should have been registered", 1, factory.getListenerContainers().size()); JmsListenerEndpoint endpoint = factory.getListenerContainers().get(0).getEndpoint(); assertEquals("Wrong endpoint type", MethodJmsListenerEndpoint.class, endpoint.getClass()); MethodJmsListenerEndpoint methodEndpoint = (MethodJmsListenerEndpoint) endpoint; assertEquals(MetaAnnotationTestBean.class, methodEndpoint.getBean().getClass()); assertEquals(MetaAnnotationTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMethod()); assertEquals(MetaAnnotationTestBean.class.getMethod("handleIt", String.class), methodEndpoint.getMostSpecificMethod()); assertEquals("metaTestQueue", ((AbstractJmsListenerEndpoint) endpoint).getDestination()); } finally { context.close(); } }
/** * Instantiate an empty {@link MethodJmsListenerEndpoint} for further * configuration with provided parameters in {@link #processJmsListener}. * @return a new {@code MethodJmsListenerEndpoint} or subclass thereof * @since 4.1.9 * @see MethodJmsListenerEndpoint#createMessageListenerInstance() */ protected MethodJmsListenerEndpoint createMethodJmsListenerEndpoint() { return new MethodJmsListenerEndpoint(); }