I am using Babbler to write an XMPP soft client for doing load testing.
In the API documentation Babbler documentation , it mentions adding listeners to intercept incoming messages. However, the example code is written in lambda form.
// Listen for messages
xmppClient.addInboundMessageListener(e -> {
Message message = e.getMessage();
// Handle inbound message.
});
I need help converting this to Java 1.7 function since our load generation tool (nGrinder) does not support lambda syntax.
Such a lambda is a just a shorthand for an anonymous implementation of a [functional] interface. You can always implement it yourself "the long way":
// Listen for messages
xmppClient.addInboundMessageListener(new Consumer<MessageEvent>() {
@Override
public void accept(MessageEvent e) {
Message message = e.getMessage();
// Handle inbound message.
}
});
// Listen for messages
xmppClient.addInboundMessageListener(new Consumer<MessageEvent>() {
public void accept(MessageEvent e) {
Message message = e.getMessage();
// Handle inbound message.
}
});
You can also avoid creating a new Consumer instance every time you invoke a certain lambda function by storing it in an instance variable.
private Consumer<MessageEvent> inboundMessageListener;
//should be called during startup only
public void initialize() {
inboundMessageListener = new Consumer<MessageEvent>() {
public void accept(MessageEvent e) {
Message message = e.getMessage();
// Handle inbound message.
}
};
}
//can be reused for more than one XMPP client, assuming there's no difference in handling different clients
public void addInboundMessageListener() {
xmppClient.addInboundMessageListener(inboundMessageListener);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.