3

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.

2
  • 1
    read the documentation provide by this platform. It have good reference. stackoverflow.com/documentation/java/91/… Commented Aug 22, 2016 at 3:02
  • Thank you for that. I will take time to go through the entire thing once I'm freed up. For the mean time, I need a quick and dirty solution. Commented Aug 22, 2016 at 3:19

2 Answers 2

4

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.
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

Brief and concise. Thanks! Just what I needed.
1
// 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);
}

5 Comments

Adding the instance variable comment is really helpful and would speed things up if I'm receiving a lot of events.
@jeffsia: that doesn’t speed up anything. The code that is executed when receiving an event is exactly the same.
@Holger of course he's not going to call the initialize() method every time. The snippet is only a sample.
@Julio D: now it makes more sense, though scenarios like this are rather unlikely. Note, how this confused the OP into thinking there was a difference when receiving an event whereas it only make a difference when registering the listener multiple times to a source.
@Holger Point taken, it doesn't really matter though at this point as I only wanted the snippet for a few tests without taking performance as a consideration.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.