public void testSendMessageOK() throws ClassNotFoundException, IllegalAccessException, InstantiationException, InterruptedException, IOException, MistException, JMSServiceException, JMSException, CODIException {
/**
* Setup open mq
*/
OpenMQTestBroker brk = new OpenMQTestBroker("test", 9876);
brk.start();
brk.registerOnZk();
for(int i = 0; i < 10; i++) {
if(brokerFarm.getBrokerCount() == 1)
break;
Utils.justSleep(500);
}
assertEquals(1, brokerFarm.getBrokerCount());
assertTrue(Utils.checkSocketConnectable("localhost", 9876));
/**
* Setup session, socket
*/
GateTalk.Session sessConfig = genSessionConfig("", "", "", "", "");
ProducerSession sess = new ProducerSession(0, sessConfig);
GateTalk.Client clientConfig = genClientConfig("foo.out");
sess.addClient(clientConfig);
sess.attach(GateTalk.Request.Role.SINK);
Socket socket = new Socket("localhost", sess.getCommPort());
for(int i = 0; i < 10; i++) {
if(sess.isReady())
break;
Utils.justSleep(500);
}
assertTrue(sess.isReady());
Packet packet = new Packet();
BufferedInputStream socketInput = new BufferedInputStream(socket.getInputStream());
BufferedOutputStream socketOutput = new BufferedOutputStream(socket.getOutputStream());
/**
* Send a message to foo.out
*/
MistMessage.MessageBlock msg = MistMessage.MessageBlock.newBuilder().setId("queue:foo.out").setMessage(ByteString.copyFrom("test".getBytes())).build();
packet.setPayload(msg.toByteArray());
packet.write(socketOutput);
packet.read(socketInput);
assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
byte[] recvMsg = brk.getMessage(true, "foo.out");
assertEquals("test", new String(recvMsg));
/**
* Send a message to bar.out
*/
msg = MistMessage.MessageBlock.newBuilder().setId("queue:bar.out").setMessage(ByteString.copyFrom("test-bar".getBytes())).build();
packet.setPayload(msg.toByteArray());
packet.write(socketOutput);
packet.read(socketInput);
assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
recvMsg = brk.getMessage(true, "bar.out");
assertEquals("test-bar", new String(recvMsg));
/**
* Test Local Forwarding: foo.out->bar.in, ,log.in
*/
Vector<Exchange> destList = new Vector<Exchange>();
destList.add(new Exchange("bar.in"));
destList.add(new Exchange(""));
destList.add(new Exchange("log.in"));
RouteFarm.getInstance().getRouteTable().put("foo.out", destList);
// Wait until local routing cache expires
Utils.justSleep(2000);
msg = MistMessage.MessageBlock.newBuilder().setId("queue:foo.out").setMessage(ByteString.copyFrom("test-route".getBytes())).build();
packet.setPayload(msg.toByteArray());
packet.write(socketOutput);
packet.read(socketInput);
assertTrue(GateTalk.Response.newBuilder().mergeFrom(packet.getPayload()).build().getSuccess());
recvMsg = brk.getMessage(true, "bar.in");
assertEquals("test-route", new String(recvMsg));
recvMsg = brk.getMessage(true, "log.in");
assertEquals("test-route", new String(recvMsg));
brk.stop();
}