Search in sources :

Example 1 with DestinationType

use of nl.nn.adapterframework.jms.JMSFacade.DestinationType in project iaf by ibissource.

the class XmlJmsBrowserSender method sendMessage.

@Override
public Message sendMessage(Message message, PipeLineSession session) throws SenderException, TimeoutException {
    Element queueBrowserElement;
    String root = null;
    String jmsRealm = null;
    String queueConnectionFactoryName = null;
    String destinationName = null;
    DestinationType destinationType = null;
    try {
        queueBrowserElement = XmlUtils.buildElement(message.asString());
        root = queueBrowserElement.getTagName();
        jmsRealm = XmlUtils.getChildTagAsString(queueBrowserElement, "jmsRealm");
        queueConnectionFactoryName = XmlUtils.getChildTagAsString(queueBrowserElement, "queueConnectionFactoryName");
        destinationName = XmlUtils.getChildTagAsString(queueBrowserElement, "destinationName");
        destinationType = EnumUtils.parse(DestinationType.class, XmlUtils.getChildTagAsString(queueBrowserElement, "destinationType"));
    } catch (Exception e) {
        throw new SenderException(getLogPrefix() + "got exception parsing [" + message + "]", e);
    }
    JmsBrowser<javax.jms.Message> jmsBrowser = createJmsBrowser();
    jmsBrowser.setName("XmlQueueBrowserSender");
    if (jmsRealm != null) {
        jmsBrowser.setJmsRealm(jmsRealm);
    }
    if (queueConnectionFactoryName != null) {
        jmsBrowser.setQueueConnectionFactoryName(queueConnectionFactoryName);
    }
    jmsBrowser.setDestinationName(destinationName);
    jmsBrowser.setDestinationType(destinationType);
    IMessageBrowsingIterator it = null;
    boolean remove = false;
    if (root.equalsIgnoreCase("browse")) {
    // OK
    } else {
        if (root.equalsIgnoreCase("remove")) {
            remove = true;
        } else {
            throw new SenderException(getLogPrefix() + "unknown root element [" + root + "]");
        }
    }
    XmlBuilder result = new XmlBuilder("result");
    XmlBuilder items;
    if (remove) {
        items = new XmlBuilder("itemsRemoved");
    } else {
        items = new XmlBuilder("items");
    }
    try {
        int count = 0;
        it = jmsBrowser.getIterator();
        while (it.hasNext()) {
            count++;
            JmsMessageBrowserIteratorItem jmsMessageBrowserIteratorItem = (JmsMessageBrowserIteratorItem) it.next();
            if (remove) {
                jmsBrowser.deleteMessage(jmsMessageBrowserIteratorItem.getJMSMessageID());
            } else {
                // browse
                XmlBuilder item = new XmlBuilder("item");
                XmlBuilder timestamp = new XmlBuilder("timestamp");
                timestamp.setValue(new java.util.Date(jmsMessageBrowserIteratorItem.getJMSTimestamp()).toString());
                item.addSubElement(timestamp);
                XmlBuilder messageId = new XmlBuilder("messageId");
                messageId.setValue(jmsMessageBrowserIteratorItem.getJMSMessageID());
                item.addSubElement(messageId);
                XmlBuilder correlationId = new XmlBuilder("correlationId");
                correlationId.setValue(jmsMessageBrowserIteratorItem.getCorrelationId());
                item.addSubElement(correlationId);
                XmlBuilder msg = new XmlBuilder("message");
                msg.setCdataValue(jmsMessageBrowserIteratorItem.getText());
                item.addSubElement(msg);
                items.addSubElement(item);
            }
        }
        if (remove) {
            items.setValue(Integer.toString(count));
        } else {
            items.addAttribute("count", count);
        }
        result.addSubElement(items);
    } catch (ListenerException e) {
        throw new SenderException(getLogPrefix() + "got exception browsing messages", e);
    } finally {
        try {
            if (it != null) {
                it.close();
            }
        } catch (ListenerException e) {
            log.warn(getLogPrefix() + "exception on closing message browser iterator", e);
        }
    }
    return new Message(result.toXML());
}
Also used : Message(nl.nn.adapterframework.stream.Message) Element(org.w3c.dom.Element) IMessageBrowsingIterator(nl.nn.adapterframework.core.IMessageBrowsingIterator) DestinationType(nl.nn.adapterframework.jms.JMSFacade.DestinationType) SenderException(nl.nn.adapterframework.core.SenderException) TimeoutException(nl.nn.adapterframework.core.TimeoutException) ListenerException(nl.nn.adapterframework.core.ListenerException) ListenerException(nl.nn.adapterframework.core.ListenerException) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) SenderException(nl.nn.adapterframework.core.SenderException)

Example 2 with DestinationType

use of nl.nn.adapterframework.jms.JMSFacade.DestinationType in project iaf by ibissource.

the class BrowseQueue method putBrowseQueue.

@POST
@RolesAllowed({ "IbisObserver", "IbisDataAdmin", "IbisAdmin", "IbisTester" })
@Path("jms/browse")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response putBrowseQueue(LinkedHashMap<String, Object> json) throws ApiException {
    Map<String, Object> returnMap = new HashMap<String, Object>();
    String connectionFactory = null, destination = null;
    boolean rowNumbersOnly = false, showPayload = false, lookupDestination = false;
    DestinationType type = null;
    for (Entry<String, Object> entry : json.entrySet()) {
        String key = entry.getKey();
        if (key.equalsIgnoreCase("connectionFactory")) {
            connectionFactory = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("destination")) {
            destination = entry.getValue().toString();
        }
        if (key.equalsIgnoreCase("type")) {
            type = EnumUtils.parse(DestinationType.class, entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("rowNumbersOnly")) {
            rowNumbersOnly = Boolean.parseBoolean(entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("payload")) {
            showPayload = Boolean.parseBoolean(entry.getValue().toString());
        }
        if (key.equalsIgnoreCase("lookupDestination")) {
            lookupDestination = Boolean.parseBoolean(entry.getValue().toString());
        }
    }
    if (connectionFactory == null)
        throw new ApiException("No connection factory provided");
    if (destination == null)
        throw new ApiException("No destination provided");
    if (type == null)
        throw new ApiException("No type provided");
    try {
        JmsBrowser<javax.jms.Message> jmsBrowser = getIbisContext().createBeanAutowireByName(JmsBrowser.class);
        jmsBrowser.setName("BrowseQueueAction");
        if (type == DestinationType.QUEUE) {
            jmsBrowser.setQueueConnectionFactoryName(connectionFactory);
        } else {
            jmsBrowser.setTopicConnectionFactoryName(connectionFactory);
        }
        jmsBrowser.setDestinationName(destination);
        jmsBrowser.setDestinationType(type);
        jmsBrowser.setLookupDestination(lookupDestination);
        List<Map<String, Object>> messages = new ArrayList<Map<String, Object>>();
        try (IMessageBrowsingIterator it = jmsBrowser.getIterator()) {
            while (it.hasNext()) {
                IMessageBrowsingIteratorItem item = it.next();
                Map<String, Object> message = new HashMap<String, Object>();
                message.put("comment", item.getCommentString());
                message.put("correlationId", item.getCorrelationId());
                try {
                    message.put("expiryDate", item.getExpiryDate());
                } catch (Exception e) {
                    log.warn("Could not get expiryDate", e);
                }
                message.put("host", item.getHost());
                message.put("id", item.getId());
                try {
                    message.put("insertDate", item.getInsertDate());
                } catch (Exception e) {
                    log.warn("Could not get insertDate", e);
                }
                if (showPayload && item instanceof JmsMessageBrowserIteratorItem) {
                    message.put("text", ((JmsMessageBrowserIteratorItem) item).getText());
                }
                messages.add(message);
            }
        }
        log.debug("Browser returned " + messages.size() + " messages");
        returnMap.put("numberOfMessages", messages.size());
        if (!rowNumbersOnly) {
            returnMap.put("messages", messages);
        }
    } catch (Exception e) {
        throw new ApiException("Error occured browsing messages", e);
    }
    return Response.status(Response.Status.OK).entity(returnMap).build();
}
Also used : JmsMessageBrowserIteratorItem(nl.nn.adapterframework.jms.JmsMessageBrowserIteratorItem) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IMessageBrowsingIteratorItem(nl.nn.adapterframework.core.IMessageBrowsingIteratorItem) ArrayList(java.util.ArrayList) IMessageBrowsingIterator(nl.nn.adapterframework.core.IMessageBrowsingIterator) DestinationType(nl.nn.adapterframework.jms.JMSFacade.DestinationType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Aggregations

IMessageBrowsingIterator (nl.nn.adapterframework.core.IMessageBrowsingIterator)2 DestinationType (nl.nn.adapterframework.jms.JMSFacade.DestinationType)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 RolesAllowed (javax.annotation.security.RolesAllowed)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 IMessageBrowsingIteratorItem (nl.nn.adapterframework.core.IMessageBrowsingIteratorItem)1 ListenerException (nl.nn.adapterframework.core.ListenerException)1 SenderException (nl.nn.adapterframework.core.SenderException)1 TimeoutException (nl.nn.adapterframework.core.TimeoutException)1 JmsMessageBrowserIteratorItem (nl.nn.adapterframework.jms.JmsMessageBrowserIteratorItem)1 Message (nl.nn.adapterframework.stream.Message)1 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)1 Element (org.w3c.dom.Element)1