Search in sources :

Example 1 with QueueInfo

use of com.tibco.tibjms.admin.QueueInfo in project iaf by ibissource.

the class GetTibcoQueues method getQueueMessage.

private String getQueueMessage(Session jSession, TibjmsAdmin admin, String queueName, int queueItem, LdapSender ldapSender) throws TibjmsAdminException, JMSException {
    QueueInfo qInfo = admin.getQueue(queueName);
    if (qInfo == null) {
        throw new JMSException(" queue [" + queueName + "] does not exist");
    }
    XmlBuilder qMessageXml = new XmlBuilder("qMessage");
    ServerInfo serverInfo = admin.getInfo();
    String url = serverInfo.getURL();
    qMessageXml.addAttribute("url", url);
    String resolvedUrl = getResolvedUrl(url);
    if (resolvedUrl != null) {
        qMessageXml.addAttribute("resolvedUrl", resolvedUrl);
    }
    qMessageXml.addAttribute("timestamp", DateUtils.getIsoTimeStamp());
    qMessageXml.addAttribute("startTime", DateUtils.format(serverInfo.getStartTime(), DateUtils.fullIsoFormat));
    XmlBuilder qNameXml = new XmlBuilder("qName");
    qNameXml.setCdataValue(queueName);
    Queue queue = jSession.createQueue(queueName);
    QueueBrowser queueBrowser = null;
    try {
        queueBrowser = jSession.createBrowser(queue);
        Enumeration enm = queueBrowser.getEnumeration();
        int count = 0;
        boolean found = false;
        String chompCharSizeString = AppConstants.getInstance().getString("browseQueue.chompCharSize", null);
        int chompCharSize = (int) Misc.toFileSize(chompCharSizeString, -1);
        while (enm.hasMoreElements() && !found) {
            count++;
            if (count == queueItem) {
                qNameXml.addAttribute("item", count);
                Object o = enm.nextElement();
                if (o instanceof Message) {
                    Message msg = (Message) o;
                    XmlBuilder qMessageId = new XmlBuilder("qMessageId");
                    qMessageId.setCdataValue(msg.getJMSMessageID());
                    qMessageXml.addSubElement(qMessageId);
                    XmlBuilder qTimestamp = new XmlBuilder("qTimestamp");
                    qTimestamp.setCdataValue(DateUtils.format(msg.getJMSTimestamp(), DateUtils.fullIsoFormat));
                    qMessageXml.addSubElement(qTimestamp);
                    StringBuffer sb = new StringBuffer("");
                    Enumeration propertyNames = msg.getPropertyNames();
                    while (propertyNames.hasMoreElements()) {
                        String propertyName = (String) propertyNames.nextElement();
                        Object object = msg.getObjectProperty(propertyName);
                        if (sb.length() > 0) {
                            sb.append("; ");
                        }
                        sb.append(propertyName);
                        sb.append("=");
                        sb.append(object);
                    }
                    XmlBuilder qPropsXml = new XmlBuilder("qProps");
                    qPropsXml.setCdataValue(sb.toString());
                    qMessageXml.addSubElement(qPropsXml);
                    XmlBuilder qTextXml = new XmlBuilder("qText");
                    String msgText;
                    try {
                        TextMessage textMessage = (TextMessage) msg;
                        msgText = textMessage.getText();
                    } catch (ClassCastException e) {
                        msgText = msg.toString();
                        qTextXml.addAttribute("text", false);
                    }
                    int msgSize = msgText.length();
                    if (isHideMessage()) {
                        qTextXml.setCdataValue("***HIDDEN***");
                    } else {
                        if (chompCharSize >= 0 && msgSize > chompCharSize) {
                            qTextXml.setCdataValue(msgText.substring(0, chompCharSize) + "...");
                            qTextXml.addAttribute("chomped", true);
                        } else {
                            qTextXml.setCdataValue(msgText);
                        }
                    }
                    qMessageXml.addSubElement(qTextXml);
                    XmlBuilder qTextSizeXml = new XmlBuilder("qTextSize");
                    qTextSizeXml.setValue(Misc.toFileSize(msgSize));
                    qMessageXml.addSubElement(qTextSizeXml);
                }
                found = true;
            } else {
                enm.nextElement();
            }
        }
    } finally {
        if (queueBrowser != null) {
            try {
                queueBrowser.close();
            } catch (JMSException e) {
                log.warn(getLogPrefix(null) + "exception on closing queue browser", e);
            }
        }
    }
    qMessageXml.addSubElement(qNameXml);
    Map aclMap = getAclMap(admin, ldapSender);
    XmlBuilder aclXml = new XmlBuilder("acl");
    XmlBuilder qInfoXml = qInfoToXml(qInfo);
    aclXml.setValue((String) aclMap.get(qInfo.getName()));
    qInfoXml.addSubElement(aclXml);
    qMessageXml.addSubElement(qInfoXml);
    Map consumersMap = getConnectedConsumersMap(admin);
    XmlBuilder consumerXml = new XmlBuilder("connectedConsumers");
    if (consumersMap.containsKey(qInfo.getName())) {
        LinkedList<String> consumers = (LinkedList<String>) consumersMap.get(qInfo.getName());
        String consumersString = listToString(consumers);
        if (consumersString != null) {
            consumerXml.setCdataValue(consumersString);
        }
    }
    qInfoXml.addSubElement(consumerXml);
    return qMessageXml.toXML();
}
Also used : QueueInfo(com.tibco.tibjms.admin.QueueInfo) Enumeration(java.util.Enumeration) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) ServerInfo(com.tibco.tibjms.admin.ServerInfo) JMSException(javax.jms.JMSException) LinkedList(java.util.LinkedList) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) Queue(javax.jms.Queue) HashMap(java.util.HashMap) Map(java.util.Map) QueueBrowser(javax.jms.QueueBrowser) TextMessage(javax.jms.TextMessage)

Example 2 with QueueInfo

use of com.tibco.tibjms.admin.QueueInfo in project iaf by ibissource.

the class SendTibcoMessage method doPipeWithTimeoutGuarded.

public String doPipeWithTimeoutGuarded(Object input, IPipeLineSession session) throws PipeRunException {
    Connection connection = null;
    Session jSession = null;
    MessageProducer msgProducer = null;
    Destination destination = null;
    String url_work;
    String authAlias_work;
    String userName_work;
    String password_work;
    String queueName_work;
    String messageProtocol_work;
    int replyTimeout_work;
    String soapAction_work;
    String result = null;
    ParameterValueList pvl = null;
    if (getParameterList() != null) {
        ParameterResolutionContext prc = new ParameterResolutionContext((String) input, session);
        try {
            pvl = prc.getValues(getParameterList());
        } catch (ParameterException e) {
            throw new PipeRunException(this, getLogPrefix(session) + "exception on extracting parameters", e);
        }
    }
    url_work = getParameterValue(pvl, "url");
    if (url_work == null) {
        url_work = getUrl();
    }
    authAlias_work = getParameterValue(pvl, "authAlias");
    if (authAlias_work == null) {
        authAlias_work = getAuthAlias();
    }
    userName_work = getParameterValue(pvl, "userName");
    if (userName_work == null) {
        userName_work = getUserName();
    }
    password_work = getParameterValue(pvl, "password");
    if (password_work == null) {
        password_work = getPassword();
    }
    queueName_work = getParameterValue(pvl, "queueName");
    if (queueName_work == null) {
        queueName_work = getQueueName();
    }
    messageProtocol_work = getParameterValue(pvl, "messageProtocol");
    if (messageProtocol_work == null) {
        messageProtocol_work = getMessageProtocol();
    }
    String replyTimeout_work_str = getParameterValue(pvl, "replyTimeout");
    if (replyTimeout_work_str == null) {
        replyTimeout_work = getReplyTimeout();
    } else {
        replyTimeout_work = Integer.parseInt(replyTimeout_work_str);
    }
    soapAction_work = getParameterValue(pvl, "soapAction");
    if (soapAction_work == null)
        soapAction_work = getSoapAction();
    if (StringUtils.isEmpty(soapAction_work) && !StringUtils.isEmpty(queueName_work)) {
        String[] q = queueName_work.split("\\.");
        if (q.length > 0) {
            if (q[0].equalsIgnoreCase("P2P") && q.length >= 4) {
                soapAction_work = q[3];
            } else if (q[0].equalsIgnoreCase("ESB") && q.length == 8) {
                soapAction_work = q[5] + "_" + q[6];
            } else if (q[0].equalsIgnoreCase("ESB") && q.length > 8) {
                soapAction_work = q[6] + "_" + q[7];
            }
        }
    }
    if (StringUtils.isEmpty(soapAction_work)) {
        log.debug(getLogPrefix(session) + "deriving default soapAction");
        try {
            URL resource = ClassUtils.getResourceURL(this, "/xml/xsl/esb/soapAction.xsl");
            TransformerPool tp = TransformerPool.getInstance(resource, true);
            soapAction_work = tp.transform(input.toString(), null);
        } catch (Exception e) {
            log.error(getLogPrefix(session) + "failed to execute soapAction.xsl");
        }
    }
    if (messageProtocol_work == null) {
        throw new PipeRunException(this, getLogPrefix(session) + "messageProtocol must be set");
    }
    if (!messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY) && !messageProtocol_work.equalsIgnoreCase(FIRE_AND_FORGET)) {
        throw new PipeRunException(this, getLogPrefix(session) + "illegal value for messageProtocol [" + messageProtocol_work + "], must be '" + REQUEST_REPLY + "' or '" + FIRE_AND_FORGET + "'");
    }
    CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work);
    try {
        TibjmsAdmin admin;
        try {
            admin = TibcoUtils.getActiveServerAdmin(url_work, cf);
        } catch (TibjmsAdminException e) {
            log.debug(getLogPrefix(session) + "caught exception", e);
            admin = null;
        }
        if (admin != null) {
            QueueInfo queueInfo;
            try {
                queueInfo = admin.getQueue(queueName_work);
            } catch (Exception e) {
                throw new PipeRunException(this, getLogPrefix(session) + " exception on getting queue info", e);
            }
            if (queueInfo == null) {
                throw new PipeRunException(this, getLogPrefix(session) + " queue [" + queueName_work + "] does not exist");
            }
            try {
                admin.close();
            } catch (TibjmsAdminException e) {
                log.warn(getLogPrefix(session) + "exception on closing Tibjms Admin", e);
            }
        }
        ConnectionFactory factory = new com.tibco.tibjms.TibjmsConnectionFactory(url_work);
        connection = factory.createConnection(cf.getUsername(), cf.getPassword());
        jSession = connection.createSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
        destination = jSession.createQueue(queueName_work);
        msgProducer = jSession.createProducer(destination);
        TextMessage msg = jSession.createTextMessage();
        msg.setText(input.toString());
        Destination replyQueue = null;
        if (messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY)) {
            replyQueue = jSession.createTemporaryQueue();
            msg.setJMSReplyTo(replyQueue);
            msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT);
            msgProducer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
            msgProducer.setTimeToLive(replyTimeout_work);
        } else {
            msg.setJMSDeliveryMode(DeliveryMode.PERSISTENT);
            msgProducer.setDeliveryMode(DeliveryMode.PERSISTENT);
        }
        if (StringUtils.isNotEmpty(soapAction_work)) {
            log.debug(getLogPrefix(session) + "setting [SoapAction] property to value [" + soapAction_work + "]");
            msg.setStringProperty("SoapAction", soapAction_work);
        }
        msgProducer.send(msg);
        if (log.isDebugEnabled()) {
            log.debug(getLogPrefix(session) + "sent message [" + msg.getText() + "] " + "to [" + msgProducer.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo() + "]");
        } else {
            if (log.isInfoEnabled()) {
                log.info(getLogPrefix(session) + "sent message to [" + msgProducer.getDestination() + "] " + "msgID [" + msg.getJMSMessageID() + "] " + "correlationID [" + msg.getJMSCorrelationID() + "] " + "replyTo [" + msg.getJMSReplyTo() + "]");
            }
        }
        if (messageProtocol_work.equalsIgnoreCase(REQUEST_REPLY)) {
            String replyCorrelationId = msg.getJMSMessageID();
            MessageConsumer msgConsumer = jSession.createConsumer(replyQueue, "JMSCorrelationID='" + replyCorrelationId + "'");
            log.debug(getLogPrefix(session) + "] start waiting for reply on [" + replyQueue + "] selector [" + replyCorrelationId + "] for [" + replyTimeout_work + "] ms");
            try {
                connection.start();
                Message rawReplyMsg = msgConsumer.receive(replyTimeout_work);
                if (rawReplyMsg == null) {
                    throw new PipeRunException(this, getLogPrefix(session) + "did not receive reply on [" + replyQueue + "] replyCorrelationId [" + replyCorrelationId + "] within [" + replyTimeout_work + "] ms");
                }
                TextMessage replyMsg = (TextMessage) rawReplyMsg;
                result = replyMsg.getText();
            } finally {
            }
        } else {
            result = msg.getJMSMessageID();
        }
    } catch (JMSException e) {
        throw new PipeRunException(this, getLogPrefix(session) + " exception on sending message to Tibco queue", e);
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (JMSException e) {
                log.warn(getLogPrefix(session) + "exception on closing connection", e);
            }
        }
    }
    return result;
}
Also used : QueueInfo(com.tibco.tibjms.admin.QueueInfo) Destination(javax.jms.Destination) ParameterValueList(nl.nn.adapterframework.parameters.ParameterValueList) TextMessage(javax.jms.TextMessage) Message(javax.jms.Message) JMSException(javax.jms.JMSException) URL(java.net.URL) TransformerPool(nl.nn.adapterframework.util.TransformerPool) ConnectionFactory(javax.jms.ConnectionFactory) ParameterException(nl.nn.adapterframework.core.ParameterException) ParameterResolutionContext(nl.nn.adapterframework.parameters.ParameterResolutionContext) MessageConsumer(javax.jms.MessageConsumer) CredentialFactory(nl.nn.adapterframework.util.CredentialFactory) Connection(javax.jms.Connection) TibjmsAdmin(com.tibco.tibjms.admin.TibjmsAdmin) PipeRunException(nl.nn.adapterframework.core.PipeRunException) JMSException(javax.jms.JMSException) TibjmsAdminException(com.tibco.tibjms.admin.TibjmsAdminException) ParameterException(nl.nn.adapterframework.core.ParameterException) TibjmsAdminException(com.tibco.tibjms.admin.TibjmsAdminException) PipeRunException(nl.nn.adapterframework.core.PipeRunException) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session) IPipeLineSession(nl.nn.adapterframework.core.IPipeLineSession)

Example 3 with QueueInfo

use of com.tibco.tibjms.admin.QueueInfo in project iaf by ibissource.

the class GetTibcoQueues method getQueuesInfo.

private String getQueuesInfo(Session jSession, TibjmsAdmin admin, boolean showAge, LdapSender ldapSender) throws TibjmsAdminException {
    XmlBuilder qInfosXml = new XmlBuilder("qInfos");
    ServerInfo serverInfo = admin.getInfo();
    String url = serverInfo.getURL();
    qInfosXml.addAttribute("url", url);
    String resolvedUrl = getResolvedUrl(url);
    if (resolvedUrl != null) {
        qInfosXml.addAttribute("resolvedUrl", resolvedUrl);
    }
    long currentTime = (new Date()).getTime();
    qInfosXml.addAttribute("timestamp", DateUtils.format(currentTime, DateUtils.fullIsoFormat));
    long startTime = serverInfo.getStartTime();
    qInfosXml.addAttribute("startTime", DateUtils.format(startTime, DateUtils.fullIsoFormat));
    qInfosXml.addAttribute("age", Misc.getAge(startTime));
    Map aclMap = getAclMap(admin, ldapSender);
    Map consumersMap = getConnectedConsumersMap(admin);
    QueueInfo[] qInfos = admin.getQueues();
    for (int i = 0; i < qInfos.length; i++) {
        QueueInfo qInfo = qInfos[i];
        if (skipTemporaryQueues && qInfo.isTemporary()) {
        // skip
        } else {
            XmlBuilder qInfoXml = qInfoToXml(qInfo);
            qInfosXml.addSubElement(qInfoXml);
            XmlBuilder aclXml = new XmlBuilder("acl");
            aclXml.setValue((String) aclMap.get(qInfo.getName()));
            qInfoXml.addSubElement(aclXml);
            XmlBuilder consumerXml = new XmlBuilder("connectedConsumers");
            if (consumersMap.containsKey(qInfo.getName())) {
                LinkedList<String> consumers = (LinkedList<String>) consumersMap.get(qInfo.getName());
                String consumersString = listToString(consumers);
                if (consumersString != null) {
                    consumerXml.setCdataValue(consumersString);
                }
            }
            qInfoXml.addSubElement(consumerXml);
            if (showAge) {
                if (qInfo.getReceiverCount() == 0 && qInfo.getPendingMessageCount() > 0) {
                    String qfmAge;
                    if (getQueueRegex() == null || qInfo.getName().matches(getQueueRegex())) {
                        qfmAge = TibcoUtils.getQueueFirstMessageAgeAsString(jSession, qInfo.getName(), currentTime);
                    } else {
                        qfmAge = "?";
                    }
                    if (qfmAge != null) {
                        XmlBuilder firstMsgAgeXml = new XmlBuilder("firstMsgAge");
                        firstMsgAgeXml.setCdataValue(qfmAge);
                        qInfoXml.addSubElement(firstMsgAgeXml);
                    }
                }
            }
        }
    }
    return qInfosXml.toXML();
}
Also used : QueueInfo(com.tibco.tibjms.admin.QueueInfo) ServerInfo(com.tibco.tibjms.admin.ServerInfo) XmlBuilder(nl.nn.adapterframework.util.XmlBuilder) HashMap(java.util.HashMap) Map(java.util.Map) Date(java.util.Date) LinkedList(java.util.LinkedList)

Example 4 with QueueInfo

use of com.tibco.tibjms.admin.QueueInfo in project iaf by ibissource.

the class GetTibcoQueues method getQueueMessageCountOnly.

private String getQueueMessageCountOnly(TibjmsAdmin admin, String queueName) throws TibjmsAdminInvalidNameException, TibjmsAdminException {
    QueueInfo queueInfo = admin.getQueue(queueName);
    long pendingMessageCount = queueInfo.getPendingMessageCount();
    return "<qCount>" + String.valueOf(pendingMessageCount) + "</qCount>";
}
Also used : QueueInfo(com.tibco.tibjms.admin.QueueInfo)

Aggregations

QueueInfo (com.tibco.tibjms.admin.QueueInfo)4 ServerInfo (com.tibco.tibjms.admin.ServerInfo)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 JMSException (javax.jms.JMSException)2 Message (javax.jms.Message)2 TextMessage (javax.jms.TextMessage)2 XmlBuilder (nl.nn.adapterframework.util.XmlBuilder)2 TibjmsAdmin (com.tibco.tibjms.admin.TibjmsAdmin)1 TibjmsAdminException (com.tibco.tibjms.admin.TibjmsAdminException)1 URL (java.net.URL)1 Date (java.util.Date)1 Enumeration (java.util.Enumeration)1 Connection (javax.jms.Connection)1 ConnectionFactory (javax.jms.ConnectionFactory)1 Destination (javax.jms.Destination)1 MessageConsumer (javax.jms.MessageConsumer)1 MessageProducer (javax.jms.MessageProducer)1 Queue (javax.jms.Queue)1