1 /*
2  * Copyright 2002-2018 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  *      http://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 
17 module hunt.stomp.support.ExecutorChannelInterceptor;
18 
19 import hunt.stomp.support.ChannelInterceptor;
20 import hunt.stomp.Message;
21 import hunt.stomp.MessageChannel;
22 
23 /**
24  * An extension of {@link ChannelInterceptor} with callbacks to intercept the
25  * asynchronous sending of a {@link hunt.stomp.Message} to
26  * a specific subscriber through an {@link java.util.concurrent.Executor}.
27  * Supported on {@link hunt.stomp.MessageChannel}
28  * implementations that can be configured with an {@code Executor}.
29  *
30  * @author Rossen Stoyanchev
31  * @since 4.1
32  * @see Message
33  * @see MessageChannel
34  * @see MessageHandler
35  */
36 abstract class ExecutorChannelInterceptor : ChannelInterceptor {
37 
38 	/**
39 	 * Invoked inside the {@link Runnable} submitted to the Executor just before
40 	 * calling the target MessageHandler to handle the message. Allows for
41 	 * modification of the Message if necessary or when {@code null} is returned
42 	 * the MessageHandler is not invoked.
43 	 * @param message the message to be handled
44 	 * @param channel the channel on which the message was sent to
45 	 * @param handler the target handler to handle the message
46 	 * @return the input message, or a new instance, or {@code null}
47 	 */
48 	
49 	MessageBase beforeHandle(MessageBase message, MessageChannel channel, MessageHandler handler) {
50 		return message;
51 	}
52 
53 	/**
54 	 * Invoked inside the {@link Runnable} submitted to the Executor after calling
55 	 * the target MessageHandler regardless of the outcome (i.e. Exception raised
56 	 * or not) thus allowing for proper resource cleanup.
57 	 * <p>Note that this will be invoked only if beforeHandle successfully completed
58 	 * and returned a Message, i.e. it did not return {@code null}.
59 	 * @param message the message handled
60 	 * @param channel the channel on which the message was sent to
61 	 * @param handler the target handler that handled the message
62 	 * @param ex any exception that may been raised by the handler
63 	 */
64 	void afterMessageHandled(MessageBase message, MessageChannel channel, MessageHandler handler,
65 			Exception ex) {
66 	}
67 
68 }