PageRenderTime 40ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/org/n3r/mina/listener/JCClientSendListener.java

https://github.com/AL-WUHC2/n3r-mina
Java | 53 lines | 42 code | 11 blank | 0 comment | 5 complexity | 580ae4d68e7c4030c0638e4588432a27 MD5 | raw file
  1. package org.n3r.mina.listener;
  2. import org.apache.commons.lang3.ArrayUtils;
  3. import org.apache.mina.core.buffer.IoBuffer;
  4. import org.apache.mina.core.session.IoSession;
  5. import org.n3r.core.joor.Reflect;
  6. import org.n3r.core.lang.RHex;
  7. import org.n3r.mina.JCBytesParserFactory;
  8. import org.n3r.mina.JCMessageListener;
  9. import org.n3r.mina.bean.JCMessage;
  10. import org.n3r.mina.client.process.IF1Process;
  11. import org.n3r.mina.client.process.JCClientProcess;
  12. import org.n3r.mina.utils.JCMessageUtils;
  13. import org.n3r.mina.utils.JCTypeUtils;
  14. import org.phw.ibatis.engine.PDao;
  15. import org.phw.ibatis.util.PDaoEngines;
  16. import com.alibaba.fastjson.JSON;
  17. public class JCClientSendListener extends JCMessageListener {
  18. private final static String SQL_XML = "org/n3r/mina/client/JCClientSQL.xml";
  19. private static PDao dao = PDaoEngines.getDao(SQL_XML, "Eop");
  20. private IoSession session;
  21. public JCClientSendListener(IoSession session) {
  22. this.session = session;
  23. }
  24. @Override
  25. public Object process(Object message, JCBytesParserFactory parser) throws Exception {
  26. JCMessage bean = (JCMessage) message;
  27. String ifNo = JCTypeUtils.getMsgTypeIFString(bean.getHead().getTypeFlag());
  28. JCClientProcess process = ifNo.equals("IF1") ? new IF1Process(dao, parser.getJobType()) :
  29. Reflect.on("org.n3r.mina.client.process." + ifNo + "Process").create(dao).<JCClientProcess> get();
  30. JCMessage result = process.process(bean, parser);
  31. logger.info("--------------------------------------------------");
  32. logger.info("[Client Send Message]: " + JSON.toJSONString(result));
  33. byte[] bytes = JCMessageUtils.messageToBytes(result, null);
  34. logger.info("[Client Send Bytes]: " + RHex.encode(bytes));
  35. logger.info("--------------------------------------------------");
  36. if (session != null && !session.isClosing()) {
  37. if (ArrayUtils.isEmpty(bytes)) session.close(true);
  38. else session.write(IoBuffer.wrap(bytes));
  39. }
  40. return bytes;
  41. }
  42. }