Search code examples
javasocketsnioapache-mina

How to properly send data via MINA?


I'm attempting to get started with MINA, and all of the examples seem to have data written to the session, rather than making use of a method that can write the same type of data over and over.

I'm trying to make use of org.apache.mina.filter.codec.demux.MessageEncoder / MessageDecoder to encode / decode messages, which will allow me to always perform the task in a central location instead of doing it inline in the code, like the examples do.

Let's say I have a ProtocolCodecFactory (which extends DemuxingProtocolCodecFactory) that has a LoginRequestEncoder (which implements MessageEncoder<LoginRequest>, and was added via the factory's addMessageEncoder method). Does that mean that instead of directly calling session.write() with the username/password data, I should instead do something like this?

LoginRequest request = new LoginRequest(username, password);
new ProtocolCodecFactory()
    .getEncoder(session)
    .encode(session, request, someProtocolEncoderOutput);

I'm not going to lie...MINA seems like it's supposed to simplify the networking process, and I'm sure it will when I get a handle on it, but I'm thoroughly confused right now.


Solution

  • It turns out you can simple send a request via IoSession.write(). Here is a simple example based upon my original question:

    LoginRequest request = new LoginRequest(username, password);
    session.write(request);