Sunday, October 29, 2017

com.ibm.mq.jms.MQQueueReceiver


https://findusages.com/search/com.ibm.mq.jms.MQQueueReceiver/receive$1

1. MQConnector.java

/**
 * Функция взаимодействия IBM BPM c MQ сервером
 *
 * @param hostName       Адрес сервера MQ
 * @param port           Порт сервера MQ
 * @param queueManager   Менеджер очередей
 * @param channel        Канал
 * @param sendQueue      Исходящая очередь
 * @param receiveQueue   Входящая очередь
 * @param messageContent Исходящее сообщение
 * @param timeout        Время ожидания ответного сообщения
 * @return Входящее сообщение
 * @throws JMSException
 */
public static String callMQ(String hostName, Integer port, String queueManager, String channel, String sendQueue, String receiveQueue, String messageContent, Integer timeout) throws JMSException {
    MQQueueConnection connection = createConnection(hostName, port, queueManager, channel);
    MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

    MQQueue queueRead = (MQQueue) session.createQueue(sendQueue);
    MQQueue queueWrite = (MQQueue) session.createQueue(receiveQueue);
    queueWrite.setTargetClient(JMSC.MQJMS_CLIENT_NONJMS_MQ);

    MQQueueSender sender = (MQQueueSender) session.createSender(queueWrite);

    JMSTextMessage outgoingMessage = (JMSTextMessage) session.createTextMessage(messageContent);
    outgoingMessage.setJMSType(MQC.MQFMT_STRING);

    byte[] correlationId = generateId();

    outgoingMessage.setJMSCorrelationIDAsBytes(correlationId);
    outgoingMessage.setJMSReplyTo(queueRead);
    outgoingMessage.setJMSMessageID(String.valueOf(System.currentTimeMillis()));

    LOGGER.info("Outgoing message");
    LOGGER.info(outgoingMessage.toString());

    connection.start();
    sender.send(outgoingMessage);

    MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queueRead, "JMSCorrelationID='ID:" + getHexString(correlationId) + "'");
    Message ingoingMessage = receiver.receive(timeout * 1000);

    if (ingoingMessage == null) {
        LOGGER.info("Ingoing message is null");
    } else {
        LOGGER.info("Ingoing message");
        LOGGER.info(ingoingMessage.toString());
    }

    String result = handleMessage(ingoingMessage);

    sender.close();
    receiver.close();
    session.close();
    connection.close();

    return result;
}


SimpleMQReceiver.java
/**
 * Main method
 *
 * @param args
 */
public static void main(String[] args) {
try {
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();

// Config
cf.setHostName("10.92.33.224");
cf.setPort(1414);
cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
cf.setQueueManager("JANUS1");
cf.setChannel("JANUS1.JANUS2.T1");

MQQueueConnection connection = (MQQueueConnection) cf.createQueueConnection();
MQQueueSession session = (MQQueueSession) connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("JANUS1.ONLINE.TOL.CONFOACK");
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);

long uniqueNumber = System.currentTimeMillis() % 1000;
JMSTextMessage message = (JMSTextMessage) session.createTextMessage("SimplePTP " + uniqueNumber);

// Start the connection
connection.start();

// sender.send(message);
// System.out.println("Sent message:\\n" + message);

JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
System.out.println("\\nReceived message:\\n" + receivedMessage);
System.out.println("\\nReceived message:\\n" + receivedMessage.getStringProperty("SENDER"));

sender.close();
receiver.close();
session.close();
connection.close();

System.out.println("\\nSUCCESS\\n");
} catch (JMSException jmsex) {
System.out.println(jmsex);
System.out.println("\\nFAILURE\\n");
} catch (Exception ex) {
System.out.println(ex);
System.out.println("\\nFAILURE\\n");
}
}

MQConnector.java
public void consume(int connectionID, boolean commit, String selector) {
MQDetails details = MQDetails.findById(connectionID);
QueueConnection connection = null;
QueueSession session = null;
try {
connection = connection(details);
session = connection.createQueueSession(true, QueueSession.SESSION_TRANSACTED);
Queue queue = session.createQueue(details.getQuqueName());

MQQueueReceiver receiver = null;
if (selector != null) {
receiver = (MQQueueReceiver) session.createReceiver(queue, selector);
} else {
receiver = (MQQueueReceiver) session.createReceiver(queue);
}
connection.start();
TextMessage message = (TextMessage) receiver.receive(1000);
if (message != null) {
System.out.println(message.getText());
}

if (commit) {
session.commit();
}
{
session.rollback();
}

} catch (JMSException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}

MQTemplate.java
public void consume(int connectionID, boolean commit, String selector) {
MQDetails details = MQDetails.findById(connectionID);
QueueConnection connection = null;
QueueSession session = null;
try {
connection = connection(details);
session = connection.createQueueSession(true, QueueSession.SESSION_TRANSACTED);
Queue queue = session.createQueue(details.getQuqueName());

MQQueueReceiver receiver = null;
if (selector != null) {
receiver = (MQQueueReceiver) session.createReceiver(queue, selector);
} else {
receiver = (MQQueueReceiver) session.createReceiver(queue);
}
connection.start();
TextMessage message = (TextMessage) receiver.receive(1000);
if (message != null) {
System.out.println(message.getText());
}

if (commit) {
session.commit();
}
{
session.rollback();
}

} catch (JMSException e) {
throw new RuntimeException(e);
} finally {
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
if (session != null) {
try {
session.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}

MainServlet.java

private static String getQueueElement() throws JMSException {

System.out.println("connecting to mq ...");
MQQueueConnectionFactory cf = null;
MQQueueConnection connection = null;
MQQueueSession session = null;
MQQueue queue = null;
// MQQueueSender sender=null;
MQQueueReceiver receiver = null;

cf = new MQQueueConnectionFactory();

// Config
cf.setHostName("macosx.hopto.org");
cf.setPort(1414);
cf.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
cf.setQueueManager("QM_mp_Komputer");
cf.setChannel("S_mp_Komputer");

connection = (MQQueueConnection) cf.createQueueConnection();
session = (MQQueueSession) connection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);

queue = (MQQueue) session.createQueue("queue:///gpw");
// sender = (MQQueueSender) session.createSender(queue);
receiver = (MQQueueReceiver) session.createReceiver(queue);

// long uniqueNumber = System.currentTimeMillis() % 1000;
// JMSTextMessage message = (JMSTextMessage)
// session.createTextMessage("SimplePTP "+ uniqueNumber);

// Start the connection
connection.start();
System.out.println("connection established.");
// sender.send(message);
// System.out.println("Sent message:\\n" + message);

JMSMessage receivedMessage = (JMSMessage) receiver.receive(10000);
System.out.println("receiving message...");
if (receivedMessage instanceof JMSTextMessage) {
JMSTextMessage txt = (JMSTextMessage) receivedMessage;
System.out.println("Message Received: " + txt.getText());
// sender.close();
receiver.close();
session.close();
connection.close();
System.out.println("\nSUCCESS\n");

return txt.getText();
} else {
System.out.println("\nReceived message:\n" + receivedMessage);
// sender.close();
receiver.close();
session.close();
connection.close();
System.out.println("\nSUCCESS\n");
if (receivedMessage == null)
return null;
return receivedMessage.toString();
}

}


No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...