Michael Bauroth wrote: > Hi, > > I'm currently developing my first server based on Mina. Now I've some > questions about practical implementation details. I hope, that somebody > can give me some useful hints: > > 1. I found the multiton package. When I'm right, then it can be used in > cases, where I want to add an own session handler to each of my > sessions. Now the questions: > > a) How can I do that (short example)? > b) Which usecase you can imagine (when I'm right, this way uses much > more resources on my computer. Right?)
You can use the multiton stuff if you prefer keeping session state as instance variables rather than using session attributes. You use it like this: connector.connect(address, new SingleSessionIoHandlerDelegate(new MySingleSessionIoHandlerFactory())); You need to implement MySingleSessionIoHandlerFactory which will create instances of your own SingleSessionIoHandler implementation. I don't think using the multiton package will be much more resource hungry than using an ordinary IoHandler which stores session state using session attributes. > > 2. I want to communicate with some hardware devices (>=5000). The > sessions will be opened ideally 24 hours a day. The devices communicate > over an ASCII protocol. The protocol has two embedded stages. The outer > stage defines the type of the message (command response, device message > ...) The inner stage contains messages of an simpler format (each line > contains one welldefined NMEA protocol entry - GPRMC, GPGGA ...) > > sample: > +cmd1 > -msg1 > -msg2 > +cmd2 > -msg3 > > I thought, that using a derived DemuxProtocolHandler could be a good > idea in this case. Now the questions: When you say DemuxProtocolHandler you are referring to the things in the org.apache.mina.filter.codec.demux package, right? > > a) Because all messages are textline based, would it be a good idea to > insert a TextLineCodec before my own handler from the view of resource > and performance costs? Yes, I think it would be a good idea. When using TextLineCodec the message object passed to your messageReceived() method will be Strings containg single lines. Later, if you find that TextLineCodec is a bottleneck you could just replace it with a custom and more optimized version and you won't have to change your IoHandler. If you want to convert the Strings into higher level objects before passing them on to your IoHandler you could override the decode() method in TextLineDecoder and do some custom processing using your own ProtocolDecoderOutput which delegates to the original ProtocolDecoderOutput once it has converted the Strings. > b) How can I handle such multistage protocols. Have you any > recommendations for me at this point (e.g. Decoder1 calls subdecoder2, > message1 contains message2 in it's body) Hmmm, I'm not sure I follow you here. Could you describe this idea more? > > 3. Because I want to write the decoded protocol values to a MySQL > database, I want to use your third use case (ThreadPoolFilter before and > after the SQL handler). I guess this is the right way. But at the moment > I can't imagine, how these filters work in this case. Do you know some > documents (or sample code) where I can study this stuff in more detail? I'm not aware of any such documentation. Anyone else? BTW, I'm assuming you are using at least MINA 0.9.3, right? HTH /Niklas
