MessageProducer publisher = publisherSession.createProducer(this.topic);
int nmax = 3;
// test receiveNoWait()
TextMessage[] msgIn = new TextMessage[nmax];
Message msg2 = null;
for (int i=0; i < nmax; i++) {
msgIn[i] = publisherSession.createTextMessage();
msgIn[i].setText("msg " + i);
publisher.send(this.topic, msgIn[i]);
}
for (int i=0; i < nmax; i++) {
msg2 = consumer.receiveNoWait();
if (!(msg2 instanceof TextMessage)) {
assertTrue("received message if of wrong type, should be TextMessage but is '" + msg2.getClass().getName() + "'", false);
}
assertEquals("receive(): messages are not the same", msgIn[i].getText(), ((TextMessage)msg2).getText());
}
msg2 = consumer.receiveNoWait();
if (msg2 != null) {
assertTrue("no message was sent, so null should have been returned here but it was " + msg.toString(), false);
}
// test receive(long)
msgIn = new TextMessage[nmax];
for (int i=0; i < nmax; i++) {
msgIn[i] = publisherSession.createTextMessage();
msgIn[i].setText("msg " + i);
publisher.send(this.topic, msgIn[i]);
}
for (int i=0; i < nmax; i++) {
msg2 = consumer.receive(200L);
if (!(msg2 instanceof TextMessage)) {
assertTrue("received message if of wrong type, should be TextMessage but is '" + msg2.getClass().getName() + "'", false);
}
assertEquals("receive(): messages are not the same", msgIn[i].getText(), ((TextMessage)msg2).getText());
}
msg2 = consumer.receive(200L);
if (msg2 != null) {
assertTrue("no message was sent, so null should have been returned here but it was " + msg.toString(), false);
}
// test receive()
msgIn = new TextMessage[nmax];
for (int i=0; i < nmax; i++) {
msgIn[i] = publisherSession.createTextMessage();
msgIn[i].setText("msg " + i);
publisher.send(this.topic, msgIn[i]);
}
for (int i=0; i < nmax; i++) {
msg2 = consumer.receive();
if (!(msg2 instanceof TextMessage)) {
assertTrue("received message if of wrong type, should be TextMessage but is '" + msg2.getClass().getName() + "'", false);
}
assertEquals("receive(): messages are not the same", msgIn[i].getText(), ((TextMessage)msg2).getText());
}
//PublisherThread pub = new PublisherThread(publisher, msg, 6, 100L);
//pub.start();