@Test public void testCreateMessageQueue2() throws InterruptedException { Set<String> messageTypeSet = new HashSet<String>(); messageTypeSet.add(TYPE); ConsumerInfo consumerInfo = new ConsumerInfo( "consumerId", Destination.topic(TOPIC_NAME), ConsumerType.DURABLE_AT_LEAST_ONCE); BlockingQueue<SwallowMessage> queue = swallowBuffer.createMessageQueue( consumerInfo, tailMessageId, tailMessageId, MessageFilter.createInSetMessageFilter(messageTypeSet)); SwallowMessage m; while ((m = queue.poll(1, TimeUnit.SECONDS)) == null) {; } Assert.assertEquals("content2", m.getContent()); }
/** * 保存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; }