środa, maja 22, 2013

Początkowa faza implementacji serwera JMS

Provider potrafiący wysłać dane na kolejkę i odbierać z kolejki. Zawartość edukacyjna.

QInitialContext ctx = new QInitialContext();
ctx.setUrl("direct://C://temp");
QConnectionFactory connFactory = (QConnectionFactory) ctx.lookup("QueueConnectionFactory");
QueueConnection conn = (QueueConnection) connFactory.createConnection();
QueueSession sess = conn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
Queue q = sess.createQueue("test123");
QueueSender sender = sess.createSender(q);
int cnt = 0;
for (int i=0; i < 10; i++)
sender.send( sess.createTextMessage(System.currentTimeMillis() + "X" + ++cnt) );
QueueReceiver receiver = sess.createReceiver(q);
TextMessage m = null;
do {
m = (TextMessage) receiver.receive();
if (m!=null) {
System.out.println(m.getJMSMessageID() + ": " + m.getText());
}
}
while (m!=null);
sess.close();
conn.close();

0 komentarze: