Introduction
Abbrevation for Mina is ‘Multipurpose Infrastructure for Network Applications’, which is a network application framework to develop highly scalable and performant network applications. In this article, let us see how to create a simple client/server application using Apache Mina 2.0.x.
Required jars
For the server part, Two classes named “MinaServer” and “MinaServerHandler” has been used. The “MinaServer” class contains a main method and an interface named “IoAcceptor” is used to accept the incoming connections from the client and that fires the event to the handler. Two filters has been used, the first one is the “LoggingFilter” which logs all the events and requests and the second one is the “ProtocolCodecFilter” which is used to convert an incoming ByteBuffer into message POJO. The code for the “MinaServer” class is given below,
MinaServer.java
Next let us create a custom handler named “MinaServerHandler”, which contains four methods. The first method “sessionOpened” is called when the session is opened and it is used to set the session idle time. The second method “receiveMessage” is used to receive the message sent by the client. The other two methods “sessionIdle” is used to close the session when it was idle for 10 secs and the fourth method “exceptionCaught” is used to close the session when an exception occured. The code for the “MinaServerHandler” class is given below,
MinaServerHandler.java
Abbrevation for Mina is ‘Multipurpose Infrastructure for Network Applications’, which is a network application framework to develop highly scalable and performant network applications. In this article, let us see how to create a simple client/server application using Apache Mina 2.0.x.
Required jars
- Apache Mina 2.0.x jars
- slf4j-api.jar
- slf4k-jdk14.jar
For the server part, Two classes named “MinaServer” and “MinaServerHandler” has been used. The “MinaServer” class contains a main method and an interface named “IoAcceptor” is used to accept the incoming connections from the client and that fires the event to the handler. Two filters has been used, the first one is the “LoggingFilter” which logs all the events and requests and the second one is the “ProtocolCodecFilter” which is used to convert an incoming ByteBuffer into message POJO. The code for the “MinaServer” class is given below,
MinaServer.java
01 | package com.sample.timeserver; |
02 |
03 | /** |
04 | * @author giftsam |
05 | */ |
06 | import java.io.IOException; |
07 | import java.net.InetSocketAddress; |
08 | import java.nio.charset.Charset; |
09 |
10 | import org.apache.mina.core.session.IdleStatus; |
11 | import org.apache.mina.core.service.IoAcceptor; |
12 | import org.apache.mina.filter.codec.ProtocolCodecFilter; |
13 | import org.apache.mina.filter.codec.textline.TextLineCodecFactory; |
14 | import org.apache.mina.filter.logging.LoggingFilter; |
15 | import org.apache.mina.transport.socket.nio.NioSocketAcceptor; |
16 |
17 | public class MinaServer |
18 | { |
19 | private static final int PORT = 1234 ; |
20 |
21 | public static void main(String[] args) throws IOException |
22 | { |
23 | IoAcceptor acceptor = new NioSocketAcceptor(); |
24 |
25 | acceptor.getFilterChain().addLast( "logger" , new LoggingFilter()); |
26 | acceptor.getFilterChain().addLast( "codec" , new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName( "UTF-8" )))); |
27 |
28 | acceptor.setHandler(new MinaServerHandler()); |
29 | acceptor.getSessionConfig().setReadBufferSize( 2048 ); |
30 | acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10 ); |
31 | acceptor.bind(new InetSocketAddress(PORT)); |
32 | } |
33 | } |
MinaServerHandler.java
01 | package com.sample.timeserver; |
02 | import org.apache.mina.core.session.IdleStatus; |
03 | import org.apache.mina.core.service.IoHandlerAdapter; |
04 | import org.apache.mina.core.session.IoSession; |
05 | import org.slf 4 j.Logger; |
06 | import org.slf 4 j.LoggerFactory; |
07 |
08 | /** |
09 | * @author giftsam |
10 | */ |
11 | public class MinaServerHandler extends IoHandlerAdapter |
12 | { |
13 | private final Logger logger = (Logger) LoggerFactory.getLogger(getClass()); |
14 |
15 | @Override |
16 | public void sessionOpened(IoSession session) |
17 | { |
18 | // set idle time to 10 seconds |
19 | session.getConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10 ); |
20 |
21 | session.setAttribute( "Values: " ); |
22 | } |
23 |
24 | @Override |
25 | public void messageReceived(IoSession session, Object message) |
26 | { |
27 | logger.info( "Message received in the server.." ); |
28 | logger.info( "Message is: " + message.toString()); |
29 | } |
30 |
31 | @Override |
32 | public void sessionIdle(IoSession session, IdleStatus status) |
33 | { |
34 | logger.info( "Disconnecting the idle." ); |
35 | // disconnect an idle client |
36 | session.close(); |
37 | } |
38 |
39 | @Override |
40 | public void exceptionCaught(IoSession session, Throwable cause) |
41 | { |
42 | // close the connection on exceptional situation |
43 | session.close(); |
44 | } |
45 |
46 | } |
No comments:
Post a Comment