@Test public void testPoll3() throws InterruptedException { // 插入1条消息 String myType = TYPE + "_"; SwallowMessage myTypeMsg = createMessage(); myTypeMsg.setType(myType); messageDAO.saveMessage(TOPIC_NAME, myTypeMsg); Set<String> messageTypeSet = new HashSet<String>(); messageTypeSet.add(myType); 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 = queue.poll(500, TimeUnit.MILLISECONDS); while (m == null) { m = queue.poll(50, TimeUnit.MILLISECONDS); } Assert.assertEquals(myType, m.getType()); }
@Before public void setUp() throws Exception { mongoClient.getMessageCollection(TOPIC_NAME).drop(); // 插入1条消息 SwallowMessage firstMsg = createMessage(); firstMsg.setContent("content1"); messageDAO.saveMessage(TOPIC_NAME, firstMsg); // 初始化tailMessageId tailMessageId = messageDAO.getMaxMessageId(TOPIC_NAME); // 添加9条Message int i = 2; while (i <= 10) { // 插入消息 SwallowMessage msg = createMessage(); msg.setContent("content" + i++); messageDAO.saveMessage(TOPIC_NAME, msg); } }
@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()); }
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; }