Search in sources :

Example 1 with AMQAnyDestination

use of org.apache.qpid.client.AMQAnyDestination in project jmeter-plugins by undera.

the class JmsUtil method runTest.

@Override
public SampleResult runTest(JavaSamplerContext ctx) {
    SampleResult result = new SampleResult();
    result.setContentType("plain/text");
    result.setDataType(SampleResult.TEXT);
    result.setDataEncoding(SampleResult.DEFAULT_HTTP_ENCODING);
    String connectionUrl = ctx.getParameter("connection.url");
    String bindingUrl = ctx.getParameter("binding.url");
    String message = ctx.getParameter("message");
    if (connectionUrl == null || "".equals(connectionUrl)) {
        result.setSuccessful(false);
        result.setResponseMessage("Connection URL cannot be empty.");
        result.setResponseCode("0xDEAD");
    } else {
        if (bindingUrl == null || "".equals(bindingUrl)) {
            result.setSuccessful(false);
            result.setResponseMessage("Binding URL cannot be empty.");
            result.setResponseCode("0xDEAD");
        } else {
            try {
                ConnectionFactory connectionFactory = new AMQConnectionFactory(connectionUrl);
                AMQBindingURL burl = new AMQBindingURL(bindingUrl);
                Destination destinationProducer = new AMQAnyDestination(burl);
                JmsTemplate sender = new JmsTemplate();
                sender.setConnectionFactory(connectionFactory);
                sender.setDefaultDestination(destinationProducer);
                BinaryMessageConverter bmc = new BinaryMessageConverter();
                sender.setMessageConverter(bmc);
                BinaryMessagepostProcessor postProcessor = new BinaryMessagepostProcessor();
                sender.setDeliveryMode(2);
                int rt = 30000;
                try {
                    rt = Integer.valueOf(ctx.getParameter("receive.timeout"));
                } catch (Exception e) {
                }
                sender.setReceiveTimeout(rt);
                String direction = ctx.getParameter("direction");
                if (direction == null || "".equals(direction)) {
                    direction = "send";
                }
                if (direction.toLowerCase().equals("send")) {
                    Map<String, String> mp = getMessageProperties(ctx.getParameter("header.properties"));
                    postProcessor.setMessageProperties(mp);
                    sender.convertAndSend((Object) message, postProcessor);
                    result.setSuccessful(true);
                    result.setResponseMessage("Message sent.");
                } else {
                    if (direction.toLowerCase().equals("receive")) {
                        System.out.println("Receive");
                        String messageSelector = ctx.getParameter("message.selector");
                        System.out.println("Selector: " + messageSelector);
                        Object obj = null;
                        if (messageSelector != null && !"".equals(messageSelector)) {
                            obj = sender.receiveSelectedAndConvert(messageSelector);
                        } else {
                            obj = sender.receiveAndConvert();
                        }
                        if (obj != null) {
                            result.setSuccessful(true);
                            result.setResponseData(obj.toString().getBytes());
                            String paramName = ctx.getParameter("header.property.reference");
                            if (paramName != null && !"".equals(paramName))
                                JMeterUtils.setProperty(paramName, concatProperties(bmc.getMessageProperties()));
                        } else {
                            result.setSuccessful(false);
                            result.setResponseData("Conection timeout".getBytes());
                        }
                    } else {
                        result.setSuccessful(false);
                        result.setResponseMessage("Unknown direction.");
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                result.setSuccessful(false);
                result.setResponseMessage("Exception");
                result.setResponseData(e.getMessage().getBytes());
            }
        }
    }
    return result;
}
Also used : AMQAnyDestination(org.apache.qpid.client.AMQAnyDestination) Destination(javax.jms.Destination) AMQBindingURL(org.apache.qpid.url.AMQBindingURL) AMQConnectionFactory(org.apache.qpid.client.AMQConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) AMQAnyDestination(org.apache.qpid.client.AMQAnyDestination) AMQConnectionFactory(org.apache.qpid.client.AMQConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) SampleResult(org.apache.jmeter.samplers.SampleResult)

Example 2 with AMQAnyDestination

use of org.apache.qpid.client.AMQAnyDestination in project candlepin by candlepin.

the class QpidQmf method runQuery.

/**
 * Indempotent method that connects to Qpid and uses QMF to find information about a given
 * targetType. The best reference about how to interact with QMF can be found here:
 *
 * https://access.redhat.com/documentation/en-US/
 * Red_Hat_Enterprise_MRG/2/html-single/Messaging_Programming_Reference/index.html
 *
 * Other concepts and basic docs here
 *
 * https://qpid.apache.org/releases/qpid-cpp-1.35.0/cpp-broker/book/ch02s02.html
 *
 * @param targetType
 * @param query
 * @return
 * @throws JMSException
 */
private List<Map<String, Object>> runQuery(String targetType, Map<Object, Object> query) throws JMSException {
    Session session = null;
    List<Map<String, Object>> result = new ArrayList<>();
    Connection connection = null;
    try {
        AMQAnyDestination qmfQueue = null;
        AMQAnyDestination responseQueue = null;
        try {
            qmfQueue = new AMQAnyDestination("qmf.default.direct/broker");
            responseQueue = new AMQAnyDestination("#reply-queue; {create:always, node:{x-declare:{auto-delete:true}}}");
        } catch (URISyntaxException e) {
            throw new RuntimeException("Couldn't create destinations", e);
        }
        connection = qpidConnection.newConnection();
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        MessageProducer sender = session.createProducer(qmfQueue);
        MessageConsumer receiver = session.createConsumer(responseQueue);
        MapMessage request = session.createMapMessage();
        request.setJMSReplyTo(responseQueue);
        request.setStringProperty("x-amqp-0-10.app-id", "qmf2");
        request.setStringProperty("qmf.opcode", "_query_request");
        request.setObject(targetType, query);
        // method name to be
        request.setObject("_what", "OBJECT");
        request.setJMSType("amqp/map");
        sender.send(request);
        Message response = receiver.receive(config.getInt(ConfigProperties.QPID_QMF_RECEIVE_TIMEOUT));
        if (response != null) {
            if (response instanceof MapMessage) {
                log.debug("Result received {}", response);
                MapMessage mm = (MapMessage) response;
                Enumeration en = mm.getMapNames();
                while (en.hasMoreElements()) {
                    Map<String, Object> next = (Map<String, Object>) mm.getObject(en.nextElement().toString());
                    result.add(next);
                }
                return result;
            } else {
                log.error("Received response in incorrect format: {}", response);
            }
        } else {
            log.error("No response received");
        }
    } catch (JMSException e) {
        throw e;
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
            if (session != null) {
                session.close();
            }
        } catch (JMSException e) {
            log.warn("Error closing the Qpid connection", e);
        }
    }
    return null;
}
Also used : MessageConsumer(javax.jms.MessageConsumer) Enumeration(java.util.Enumeration) MapMessage(javax.jms.MapMessage) Message(javax.jms.Message) MapMessage(javax.jms.MapMessage) ArrayList(java.util.ArrayList) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) URISyntaxException(java.net.URISyntaxException) AMQAnyDestination(org.apache.qpid.client.AMQAnyDestination) MessageProducer(javax.jms.MessageProducer) Map(java.util.Map) Session(javax.jms.Session)

Aggregations

AMQAnyDestination (org.apache.qpid.client.AMQAnyDestination)2 URISyntaxException (java.net.URISyntaxException)1 ArrayList (java.util.ArrayList)1 Enumeration (java.util.Enumeration)1 Map (java.util.Map)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 Destination (javax.jms.Destination)1 JMSException (javax.jms.JMSException)1 MapMessage (javax.jms.MapMessage)1 Message (javax.jms.Message)1 MessageConsumer (javax.jms.MessageConsumer)1 MessageProducer (javax.jms.MessageProducer)1 Session (javax.jms.Session)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1 AMQConnectionFactory (org.apache.qpid.client.AMQConnectionFactory)1 AMQBindingURL (org.apache.qpid.url.AMQBindingURL)1 JmsTemplate (org.springframework.jms.core.JmsTemplate)1