private static SwallowMessage createMessage() { SwallowMessage message = new SwallowMessage(); message.setContent("this is a SwallowMessage"); message.setGeneratedTime(new Date()); HashMap<String, String> map = new HashMap<String, String>(); map.put("property-key", "property-value"); message.setProperties(map); message.setSha1("sha-1 string"); message.setVersion("0.6.0"); message.setType(TYPE); return message; }
/** * 保存swallowMessage到数据库 * * @throws ServerDaoException */ @Override public Packet sendMessage(Packet pkt) throws ServerDaoException { if (pkt == null) { throw new IllegalArgumentException("Argument of remote service could not be null."); } Packet pktRet = null; SwallowMessage swallowMessage; String topicName; String sha1; switch (pkt.getPacketType()) { case PRODUCER_GREET: LOGGER.info( "[Got Greet][From=" + ((PktProducerGreet) pkt).getProducerIP() + "][Version=" + ((PktProducerGreet) pkt).getProducerVersion() + "]"); // 返回ProducerServer地址 pktRet = new PktSwallowPACK(producerServerIP); break; case OBJECT_MSG: topicName = ((PktMessage) pkt).getDestination().getName(); // 验证topicName是否在白名单里 boolean isValid = topicWhiteList.isValid(topicName); if (!isValid) { throw new IllegalArgumentException( "Invalid topic(" + topicName + "), because it's not in whitelist, please contact swallow group for support."); } swallowMessage = ((PktMessage) pkt).getContent(); sha1 = SHAUtil.generateSHA(swallowMessage.getContent()); pktRet = new PktSwallowPACK(sha1); // 设置swallowMessage的sha-1 swallowMessage.setSha1(sha1); String parentDomain; try { parentDomain = MessageId.parse(((PktMessage) pkt).getCatEventID()).getDomain(); } catch (Exception e) { parentDomain = "UnknownDomain"; } // MessageTree tree = Cat.getManager().getThreadLocalMessageTree(); // tree.setMessageId(((PktMessage)pkt).getCatEventID()); Transaction producerServerTransaction = Cat.getProducer() .newTransaction( "In:" + topicName, parentDomain + ":" + swallowMessage.getSourceIp()); // 将swallowMessage保存到mongodb try { messageDAO.saveMessage(topicName, swallowMessage); producerServerTransaction.addData("sha1", swallowMessage.getSha1()); producerServerTransaction.setStatus(Message.SUCCESS); } catch (Exception e) { producerServerTransaction.addData(swallowMessage.toKeyValuePairs()); producerServerTransaction.setStatus(e); Cat.getProducer().logError(e); LOGGER.error("[Save message to DB failed.]", e); throw new ServerDaoException(e); } finally { producerServerTransaction.complete(); } break; default: LOGGER.warn("[Received unrecognized packet.]" + pkt); break; } return pktRet; }