|
| 1 | +/* |
| 2 | + * Copyright 2013-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.awspring.cloud.sqs.config; |
| 17 | + |
| 18 | +import io.awspring.cloud.sqs.annotation.SqsHandler; |
| 19 | +import io.awspring.cloud.sqs.listener.MessageListener; |
| 20 | +import io.awspring.cloud.sqs.listener.MessageListenerContainer; |
| 21 | +import io.awspring.cloud.sqs.listener.adapter.DelegatingInvocableHandler; |
| 22 | +import io.awspring.cloud.sqs.listener.adapter.MessagingMessageListenerAdapter; |
| 23 | +import java.lang.reflect.Method; |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.List; |
| 27 | +import org.springframework.lang.Nullable; |
| 28 | +import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory; |
| 29 | +import org.springframework.messaging.handler.invocation.InvocableHandlerMethod; |
| 30 | + |
| 31 | +/** |
| 32 | + * A {@link SqsEndpoint} extension for multiple methods using {@link SqsHandler} |
| 33 | + * |
| 34 | + * @see SqsHandler |
| 35 | + */ |
| 36 | +public class MultiMethodSqsEndpoint extends AbstractEndpoint { |
| 37 | + |
| 38 | + private Endpoint endpoint; |
| 39 | + |
| 40 | + private List<Method> methods; |
| 41 | + |
| 42 | + private @Nullable Method defaultMethod; |
| 43 | + |
| 44 | + protected MultiMethodSqsEndpoint(MultiMethodSqsEndpointBuilder builder) { |
| 45 | + super(builder.queueNames, builder.factoryName, builder.id); |
| 46 | + this.methods = builder.methods; |
| 47 | + this.defaultMethod = builder.defaultMethod; |
| 48 | + this.endpoint = builder.endpoint; |
| 49 | + this.setBean(builder.bean); |
| 50 | + } |
| 51 | + |
| 52 | + public static MultiMethodSqsEndpointBuilder builder() { |
| 53 | + return new MultiMethodSqsEndpointBuilder(); |
| 54 | + } |
| 55 | + |
| 56 | + public Endpoint getEndpoint() { |
| 57 | + return endpoint; |
| 58 | + } |
| 59 | + |
| 60 | + public static class MultiMethodSqsEndpointBuilder { |
| 61 | + |
| 62 | + private List<Method> methods; |
| 63 | + |
| 64 | + private @Nullable Method defaultMethod; |
| 65 | + |
| 66 | + private Object bean; |
| 67 | + |
| 68 | + private String id; |
| 69 | + |
| 70 | + private Collection<String> queueNames; |
| 71 | + |
| 72 | + private String factoryName; |
| 73 | + |
| 74 | + private Endpoint endpoint; |
| 75 | + |
| 76 | + public MultiMethodSqsEndpointBuilder methods(List<Method> methods) { |
| 77 | + this.methods = methods; |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + public MultiMethodSqsEndpointBuilder defaultMethod(@Nullable Method defaultMethod) { |
| 82 | + this.defaultMethod = defaultMethod; |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + public MultiMethodSqsEndpointBuilder bean(Object bean) { |
| 87 | + this.bean = bean; |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public MultiMethodSqsEndpointBuilder queueNames(Collection<String> queueNames) { |
| 92 | + this.queueNames = queueNames; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public MultiMethodSqsEndpointBuilder factoryBeanName(String factoryName) { |
| 97 | + this.factoryName = factoryName; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public MultiMethodSqsEndpointBuilder sqsEndpoint(Endpoint sqsEndpoint) { |
| 102 | + this.endpoint = sqsEndpoint; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + public MultiMethodSqsEndpointBuilder id(String id) { |
| 107 | + this.id = id; |
| 108 | + return this; |
| 109 | + } |
| 110 | + |
| 111 | + public MultiMethodSqsEndpoint build() { |
| 112 | + return new MultiMethodSqsEndpoint(this); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public List<Method> getMethods() { |
| 117 | + return methods; |
| 118 | + } |
| 119 | + |
| 120 | + public void setMethods(List<Method> methods) { |
| 121 | + this.methods = methods; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public <T> void setupContainer(MessageListenerContainer<T> container) { |
| 126 | + List<InvocableHandlerMethod> invocableHandlerMethods = new ArrayList<>(); |
| 127 | + InvocableHandlerMethod defaultHandler = null; |
| 128 | + |
| 129 | + for (Method method : methods) { |
| 130 | + MessageHandlerMethodFactory messageHandlerMethodFactory = getMessageHandlerMethodFactory(); |
| 131 | + if (messageHandlerMethodFactory != null) { |
| 132 | + InvocableHandlerMethod invocableHandlerMethod = messageHandlerMethodFactory |
| 133 | + .createInvocableHandlerMethod(getBean(), method); |
| 134 | + invocableHandlerMethods.add(invocableHandlerMethod); |
| 135 | + if (method.equals(defaultMethod)) { |
| 136 | + defaultHandler = invocableHandlerMethod; |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + DelegatingInvocableHandler delegatingInvocableHandler = new DelegatingInvocableHandler(invocableHandlerMethods, |
| 142 | + defaultHandler); |
| 143 | + |
| 144 | + container.setMessageListener(createMessageListenerInstance(delegatingInvocableHandler)); |
| 145 | + } |
| 146 | + |
| 147 | + protected <T> MessageListener<T> createMessageListenerInstance(DelegatingInvocableHandler delegatingHandler) { |
| 148 | + return new MessagingMessageListenerAdapter<>(delegatingHandler); |
| 149 | + } |
| 150 | +} |
0 commit comments