Improve Interceptor APIs to supprt java8 lambda style
liyue2008 opened this issue · 2 comments
liyue2008 commented
For the intercepter APIs .
io.openmessaging.interceptor.ProducerInterceptor
io.openmessaging.interceptor.ConsumerInterceptor
It would be convenient for user to support java8 lambda style, that also make code more elegant.
Separating the preSend and the postSend method gives user more chorices.
Take a look the following example code:
Before:
ProducerInterceptor interceptor = new ProducerInterceptor() {
@Override
public void preSend(Message message, Context attributes) {
System.out.println("PreSend message: " + message);
}
@Override
public void postSend(Message message, Context attributes) {
System.out.println("PostSend message: " + message);
}
};
producer.addInterceptor(interceptor);
After:
producer.addPreSendInterceptor((message, attributes) ->
System.out.println("PreSend message: " + message));
producer.addPostSendInterceptor((message, attributes) ->
System.out.println("PostSend message: " + message));
duhenglucky commented
IMO, the lambda expression is only syntactic sugar, and we shouldn't binding with a certain Java version so users can write code in a way they like.
liyue2008 commented
Binding the API to java 8 is not a must to support lambda style.
To achive this propose, only a sinlge method message interceptor interface will be introduced.
/**
* A {@code Interceptor} is used to intercept send or consume operations.
* <p>
* The interceptor is able to view or modify the message being transmitted and collect
* the send record.
*
* @version OMS 1.0.0
* @since OMS 1.0.0
*/
public interface MessageInterceptor {
/**
* Invoked during send or consume operations.
*
* @param message the message is actually processing.
* @param attributes the extensible attributes delivered to the intercept thread.
*/
void onMessage(Message message, Context attributes);
}
For java versions before 8, The old way of creating an implementation class is still available.
Code chould be like this:
MessageInterceptor preSendInterceptor;
producer.addPreSendInterceptor(preSendInterceptor = new MessageInterceptor() {
@Override
public void onMessage(Message message, Context attributes) {
System.out.println("PreSend message: " + message);
}
});
producer.removeInterceptor(preSendInterceptor);