Этот код должен читать из очереди, если очередь пуста, то повторить чтение по таймеру, проверить формат текста или байта.Правильно ли в этом формате проверять пустую очередь?И будет ли работать таймер в этом случае?Пока невозможно проверить, поскольку очередь MQ не настроена.
public class Main {
public static void main(String[] args) {
boolean tru = true;
try {
/*MQ Конфигурация подключения*/
MQQueueConnectionFactory mqQueueConnectionFactory = new MQQueueConnectionFactory();
mqQueueConnectionFactory.setHostName("localhost");
mqQueueConnectionFactory.setChannel("SVRCONN");
mqQueueConnectionFactory.setPort(1414);
mqQueueConnectionFactory.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
mqQueueConnectionFactory.setQueueManager("MQ_APPLE");
mqQueueConnectionFactory.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
QueueConnection queueConnection = mqQueueConnectionFactory.createQueueConnection("name", "pass");
MQQueueSession session = (MQQueueSession) queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///Q1");
MQQueueReceiver receiver = (MQQueueReceiver) session.createReceiver(queue);
queueConnection.start();
while(tru){
TextMessage receivedMessage = (TextMessage) receiver.receive();
if(receivedMessage != null){
MStart(receivedMessage);
tru = false;
} else {
Timer timer = new Timer(10000,new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("Timer Run");
}
});
timer.start();
}
}
receiver.close();
session.close();
queueConnection.close();
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void MStart(TextMessage receivedMessage) throws JMSException {
if (receivedMessage instanceof BytesMessage) {
TextMessage textMessage = (TextMessage) receivedMessage;
System.out.println("Received message '"
+ textMessage.getText() + "'");
} else if (receivedMessage instanceof TextMessage) {
System.out.println("Received message: " + receivedMessage.getText());
}
}
}