use of com.tibco.tibjms.admin.TibjmsAdmin 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;
}
use of com.tibco.tibjms.admin.TibjmsAdmin in project iaf by ibissource.
the class TibcoUtils method getActiveServerAdmin.
protected static TibjmsAdmin getActiveServerAdmin(String url, CredentialFactory cf) throws TibjmsAdminException {
TibjmsAdminException lastException = null;
TibjmsAdmin admin = null;
String[] uws = url.split(",");
String uw = null;
boolean uws_ok = false;
for (int i = 0; !uws_ok && i < uws.length; i++) {
uw = uws[i].trim();
int state = ServerInfo.SERVER_ACTIVE * -1;
try {
// The next line of code has been reported to throw the
// following exception:
// com.tibco.tibjms.admin.TibjmsAdminException: Unable to connect to server. Root cause:
// javax.jms.ResourceAllocationException: too many open connections
admin = new TibjmsAdmin(uw, cf.getUsername(), cf.getPassword());
// The next line of code has been reported to throw the
// following exception:
// com.tibco.tibjms.admin.TibjmsAdminSecurityException: Command unavailable on a server not in active state and using a JSON configuration file
state = admin.getInfo().getState();
} catch (TibjmsAdminException e) {
// In case a passive or broken server is tried before an active
// server this will result in an exception. Hence, ignore all
// exceptions unless all servers fail in which case the latest
// exception should be logged to give an indication of what is
// going wrong.
lastException = e;
}
if (admin != null) {
if (state == ServerInfo.SERVER_ACTIVE) {
uws_ok = true;
} else {
log.debug("Server [" + uw + "] is not active, state [" + admin.getInfo().getState() + "]");
try {
admin.close();
} catch (TibjmsAdminException e) {
log.warn("Exception on closing Tibjms Admin on server [" + uw + "]", e);
}
}
}
}
if (!uws_ok) {
log.warn("Could not find an active server", lastException);
return null;
} else {
log.debug("Found active server [" + uw + "]");
return admin;
}
}
use of com.tibco.tibjms.admin.TibjmsAdmin in project iaf by ibissource.
the class GetTibcoQueues method doPipeWithTimeoutGuarded.
public String doPipeWithTimeoutGuarded(Object input, IPipeLineSession session) throws PipeRunException {
String result;
String url_work;
String authAlias_work;
String userName_work;
String password_work;
String queueName_work = 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();
}
CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work);
Connection connection = null;
Session jSession = null;
TibjmsAdmin admin = null;
try {
admin = TibcoUtils.getActiveServerAdmin(url_work, cf);
if (admin == null) {
throw new PipeRunException(this, "could not find an active server");
}
String ldapUrl = getParameterValue(pvl, "ldapUrl");
LdapSender ldapSender = null;
if (StringUtils.isNotEmpty(ldapUrl)) {
ldapSender = retrieveLdapSender(ldapUrl, cf);
}
queueName_work = getParameterValue(pvl, "queueName");
if (StringUtils.isNotEmpty(queueName_work)) {
String countOnly_work = getParameterValue(pvl, "countOnly");
boolean countOnly = ("true".equalsIgnoreCase(countOnly_work) ? true : false);
if (countOnly) {
return getQueueMessageCountOnly(admin, queueName_work);
}
}
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);
if (StringUtils.isNotEmpty(queueName_work)) {
String queueItem_work = getParameterValue(pvl, "queueItem");
int qi;
if (StringUtils.isNumeric(queueItem_work)) {
qi = Integer.parseInt(queueItem_work);
} else {
qi = 1;
}
result = getQueueMessage(jSession, admin, queueName_work, qi, ldapSender);
} else {
String showAge_work = getParameterValue(pvl, "showAge");
boolean showAge = ("true".equalsIgnoreCase(showAge_work) ? true : false);
result = getQueuesInfo(jSession, admin, showAge, ldapSender);
}
} catch (Exception e) {
String msg = getLogPrefix(session) + "exception on showing Tibco queues, url [" + url_work + "]" + (StringUtils.isNotEmpty(queueName_work) ? " queue [" + queueName_work + "]" : "");
throw new PipeRunException(this, msg, e);
} finally {
if (admin != null) {
try {
admin.close();
} catch (TibjmsAdminException e) {
log.warn(getLogPrefix(session) + "exception on closing Tibjms Admin", e);
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
log.warn(getLogPrefix(session) + "exception on closing connection", e);
}
}
}
return result;
}
Aggregations