public void testRedelivery() throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Producer p = s.createProducer(QUEUE, QoS.AT_MOST_ONCE); p.send(msg()); p.close(); Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); AMQPMessage m1 = c.receive(); assertTrue(m1.getHeader().getFirstAcquirer().getValue()); assertFalse(m1.isSettled()); s.close(); s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); AMQPMessage m2 = c.receive(); m2.accept(); assertTrue(compareMessageData(m1, m2)); assertFalse(m2.getHeader().getFirstAcquirer().getValue()); assertNull(get(c)); conn.close(); }
private void route(String consumerSource, String producerTarget, String routingKey, boolean succeed) throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Consumer c = s.createConsumer(consumerSource, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); Producer p = s.createProducer(producerTarget, QoS.AT_LEAST_ONCE); AMQPMessage msg = msg(); AmqpValue sentinel = new AmqpValue(new AMQPDouble(Math.random())); msg.setAmqpValue(sentinel); Properties props = new Properties(); props.setSubject(new AMQPString(routingKey)); msg.setProperties(props); p.send(msg); if (succeed) { AMQPMessage m = c.receive(); assertNotNull(m); assertEquals(sentinel.getValue().getValueString(), m.getAmqpValue().getValue().getValueString()); m.accept(); } else { assertNull(get(c)); } c.close(); p.close(); conn.close(); }
private MessageConsumer establishConsumer(String broker, ActiveMQDestination consumerQueue) throws Exception { BrokerItem item = brokers.get(broker); Connection c = item.createConnection(); c.start(); Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); return s.createConsumer(consumerQueue); }
private void emptyQueue(String q) throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Consumer c = s.createConsumer(q, CONSUMER_LINK_CREDIT, QoS.AT_MOST_ONCE, false, null); AMQPMessage m; while ((m = get(c)) != null); conn.close(); }
public void testAtMostOnce() throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Producer p = s.createProducer(QUEUE, QoS.AT_MOST_ONCE); p.send(msg()); p.close(); Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_MOST_ONCE, false, null); AMQPMessage m = c.receive(); assertTrue(m.isSettled()); s.close(); s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_MOST_ONCE, false, null); assertNull(get(c)); conn.close(); }
public void testRoundTrip() throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE); p.send(msg()); p.close(); // Settlement happens here Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); AMQPMessage m = c.receive(); m.accept(); assertEquals(1, m.getData().size()); assertEquals(data(), m.getData().get(0)); conn.close(); }
private void routeInvalidSource(String consumerSource) throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); try { Consumer c = s.createConsumer(consumerSource, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); c.close(); fail("Source '" + consumerSource + "' should fail"); } catch (Exception e) { // no-op } finally { conn.close(); } }
private void decorationTest(DecorationProtocol d, Map<AMQPString, AMQPType> map) throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE); AMQPMessage msg = msg(); d.decorateMessage(msg, map); p.send(msg); p.close(); Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); AMQPMessage recvMsg = c.receive(); recvMsg.accept(); compareMaps(map, d.getDecoration(recvMsg)); conn.close(); }
public void fragmentation(long FrameSize, int PayloadSize) throws UnsupportedProtocolVersionException, AMQPException, AuthenticationException, IOException { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.setMaxFrameSize(FrameSize); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE); AMQPMessage msg = new AMQPMessage(); msg.addData(new Data(new byte [PayloadSize])); p.send(msg); p.close(); Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); AMQPMessage m = c.receive(); m.accept(); c.close(); assertEquals(PayloadSize, m.getData().get(0).getValue().length); conn.close(); }
public void testDataTypes() throws Exception { AMQPContext ctx = new AMQPContext(AMQPContext.CLIENT); Connection conn = new Connection(ctx, host, port, false); conn.connect(); Session s = conn.createSession(INBOUND_WINDOW, OUTBOUND_WINDOW); Producer p = s.createProducer(QUEUE, QoS.AT_LEAST_ONCE); AMQPMessage msg = new AMQPMessage(); List<AMQPType> al = new ArrayList<AMQPType>(); al.add(new AMQPBoolean(true)); al.add(new AMQPByte(Byte.MAX_VALUE)); al.add(new AMQPChar(Character.CURRENCY_SYMBOL)); al.add(new AMQPDecimal64(BigDecimal.TEN)); al.add(new AMQPDouble(Double.NaN)); al.add(new AMQPInt(Integer.MIN_VALUE)); al.add(new AMQPNull()); al.add(new AMQPString("\uFFF9")); al.add(new AMQPSymbol(new String(new char[256]))); al.add(new AMQPTimestamp(Long.MAX_VALUE)); al.add(new AMQPUuid(System.currentTimeMillis(), Long.MIN_VALUE)); al.add(new AMQPUnsignedShort(0)); al.add(new AMQPArray(AMQPBoolean.FALSE.getCode(), new AMQPBoolean[]{})); al.add(new AmqpSequence(new ArrayList<AMQPType>())); AmqpSequence seq = new AmqpSequence(al); AmqpValue val = new AmqpValue(seq); msg.setAmqpValue(val); p.send(msg); p.close(); Consumer c = s.createConsumer(QUEUE, CONSUMER_LINK_CREDIT, QoS.AT_LEAST_ONCE, false, null); AMQPMessage recvMsg = c.receive(); recvMsg.accept(); assertEquals(val.getValue().getValueString(), recvMsg.getAmqpValue().getValue().getValueString()); conn.close(); }
@Override public int run() throws Exception { // Default values of command line arguments String host = DEFAULT_HOST; int port = DEFAULT_PORT; String user = DEFAULT_USER; String pass = DEFAULT_PASS; String destination = DEFAULT_DESTINATION; String file = ""; // No default -- if not given, don't read/write file int sleep = 0; boolean showpercent = false; int batchSize = 0; int length = 500; // Length of message generated internally String properties = ""; String format = "short"; String durable = null; int n = 1; // n is the number of messages to process, or a specific // message number, depending on content String url = ""; // No default -- if not given, don't use it String[] nonSwitchArgs = cl.getArgs(); if (nonSwitchArgs.length > 0) n = Integer.parseInt(nonSwitchArgs[0]); String _destination = cl.getOptionValue("destination"); if (_destination != null) destination = _destination; String _host = cl.getOptionValue("host"); if (_host != null) host = _host; String _port = cl.getOptionValue("port"); if (_port != null) port = Integer.parseInt(_port); String _file = cl.getOptionValue("file"); if (_file != null) file = _file; String _user = cl.getOptionValue("user"); if (_user != null) user = _user; String _pass = cl.getOptionValue("password"); if (_pass != null) pass = _pass; String _url = cl.getOptionValue("url"); if (_url != null) url = _url; String _sleep = cl.getOptionValue("sleep"); if (_sleep != null) sleep = Integer.parseInt(_sleep); if (cl.hasOption("percent")) showpercent = true; String _batchSize = cl.getOptionValue("batch"); if (_batchSize != null) batchSize = Integer.parseInt(_batchSize); String _L = cl.getOptionValue("length"); if (_L != null) length = Integer.parseInt(_L); String _properties = cl.getOptionValue("properties"); if (_properties != null) properties = _properties; String _durable = cl.getOptionValue("durable"); if (_durable != null) durable = _durable; boolean batch = false; if (batchSize != 0) batch = true; String _format = cl.getOptionValue("format"); if (_format != null) format = _format; ActiveMQConnectionFactory factory = getFactory(host, port, url); Connection connection = factory.createConnection(user, pass); if (durable != null) connection.setClientID(durable); connection.start(); Session session = connection.createSession(batch, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic(destination); MessageConsumer consumer = null; if (durable != null) consumer = session.createDurableSubscriber(topic, "amqutil"); else consumer = session.createConsumer(topic); int oldpercent = 0; for (int i = 0; i < n; i++) { javax.jms.Message message = consumer.receive(); if (batch) if ((i + 1) % batchSize == 0) session.commit(); if (sleep != 0) Thread.sleep(sleep); JMSUtil.outputMessage(format, message, file); if (showpercent) { int percent = i * 100 / n; if (percent != oldpercent) System.out.println("" + percent + "%"); oldpercent = percent; } } if (batch) session.commit(); connection.close(); return 0; }
@Test public void testFetchLast() { try { String neId = "1005255"; // KLNNMS02(cpuusage=YES cpu1min=YES memutil=YES) KLNNMS05(cpuusage=YES // bususage=YES) // String neId = "1006119"; // KLNNMS02(cpuusage=YES cpu1min=YES memutil=YES // KLNNMS05(cpuusage=NO bususage=NO) Set<String> rras = new HashSet<String>(); // klnnms02 rras.add("cpu5sec"); rras.add("cpu1min"); rras.add("memutil"); // klnnms05 rras.add("cpuusage"); rras.add("bususage"); FetchLastCommandMessage message = CommandMessageFactory.createRRDLastCommandMessage(neId, "AVERAGE", 0, 0, null, rras); MessageProducer producer = null; MessageConsumer consumer = null; // time to send the JMS request try { TextMessage reqMsg, replyMsg; producer = session.createProducer(new HornetQQueue(SERVICE_QUEUE)); // this will uniquelly identify the request String UIID = UUID.randomUUID().toString(); reqMsg = session.createTextMessage(); reqMsg.setStringProperty("ServiceRRD_msg_type", "fetchLast"); reqMsg.setStringProperty("ServiceRRD_correlation_id", UIID); String body = JsonUtil.getInstance().toJSON(message); reqMsg.setText(body); logger.info("SEND:\n" + body); producer.send(reqMsg); consumer = session.createConsumer( new HornetQQueue(SERVICE_REPLY_QUEUE), "ServiceRRD_correlation_id = '" + UIID + "'"); replyMsg = (TextMessage) consumer.receive(30000); if (replyMsg == null) { logger.info("ServiceRRD timeout on receive()"); } else { logger.info("REPLY:\n" + replyMsg.getText()); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (producer != null) producer.close(); if (consumer != null) consumer.close(); } catch (JMSException e) { } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }
// @Test public void testFetchGraphSimple() { try { String neId = "21100799"; String group = "bw"; // interesting for the BW String titleX = "Bandwidth"; String titleY = "bps"; int timespan = 0; // Daily /* String neId = "1005255"; String group = "cpu"; // interesting for the CPU String titleX = "CPU Utilization"; String titleY = "Utilization"; int timespan = 0; // Daily */ FetchGraphSimpleCommandMessage message = CommandMessageFactory.createRRDGraphSimpleCommandMessage( neId, group, timespan, titleX, titleY); MessageProducer producer = null; MessageConsumer consumer = null; try { // time to send the JMS request TextMessage reqMsg; Message replyMsg; producer = session.createProducer(new HornetQQueue(SERVICE_QUEUE)); // this will uniquelly identify the request String UIID = UUID.randomUUID().toString(); reqMsg = session.createTextMessage(); reqMsg.setStringProperty("ServiceRRD_msg_type", "fetchGraphSimple"); reqMsg.setStringProperty("ServiceRRD_correlation_id", UIID); String body = JsonUtil.getInstance().toJSON(message); reqMsg.setText(body); logger.info("SEND:\n" + body); producer.send(reqMsg); consumer = session.createConsumer( new HornetQQueue(SERVICE_REPLY_QUEUE), "ServiceRRD_correlation_id = '" + UIID + "'"); replyMsg = consumer.receive(30000); if (replyMsg == null) { logger.info("ServiceRRD timeout on receive()"); } else { if (replyMsg instanceof BytesMessage) { BytesMessage graphStream = (BytesMessage) replyMsg; byte[] graph = new byte[(int) graphStream.getBodyLength()]; graphStream.readBytes(graph); FileOutputStream image = new FileOutputStream( "/Users/cvasilak/Temp/svc-rrd-images/" + neId + "_" + group + "_" + timespan + ".png"); image.write(graph); image.close(); logger.info("image retrieved and saved!"); } else if (replyMsg instanceof TextMessage) { // the server responded with an error logger.info(((TextMessage) replyMsg).getText()); } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (producer != null) producer.close(); if (consumer != null) consumer.close(); } catch (JMSException e) { } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }