@Test public void testExecute() throws SubCommandException { PrintStream out = System.out; ByteArrayOutputStream bos = new ByteArrayOutputStream(); System.setOut(new PrintStream(bos)); Options options = ServerUtil.buildCommandlineOptions(new Options()); String[] subargs = new String[] {"-t mytopic","-p 'send message test'","-c tagA","-k order-16546745756"}; CommandLine commandLine = ServerUtil.parseCmdLine("mqadmin " + sendMessageCommand.commandName(), subargs, sendMessageCommand.buildCommandlineOptions(options), new PosixParser()); sendMessageCommand.execute(commandLine, options, null); subargs = new String[] {"-t mytopic","-p 'send message test'","-c tagA","-k order-16546745756","-b brokera","-i 1"}; commandLine = ServerUtil.parseCmdLine("mqadmin " + sendMessageCommand.commandName(), subargs, sendMessageCommand.buildCommandlineOptions(options), new PosixParser()); sendMessageCommand.execute(commandLine, options, null); System.setOut(out); String s = new String(bos.toByteArray()); Assert.assertTrue(s.contains("SEND_OK")); }
@Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { Message msg = null; String topic = commandLine.getOptionValue('t').trim(); String body = commandLine.getOptionValue('p').trim(); String tag = null; String keys = null; String brokerName = null; int queueId = -1; try { if (commandLine.hasOption('k')) { keys = commandLine.getOptionValue('k').trim(); } if (commandLine.hasOption('c')) { tag = commandLine.getOptionValue('c').trim(); } if (commandLine.hasOption('b')) { brokerName = commandLine.getOptionValue('b').trim(); } if (commandLine.hasOption('i')) { if (!commandLine.hasOption('b')) { System.out.print("Broker name must be set if the queue is chosen!"); return; } else { queueId = Integer.parseInt(commandLine.getOptionValue('i').trim()); } } msg = new Message(topic, tag, keys, body.getBytes("utf-8")); } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } DefaultMQProducer producer = this.createProducer(rpcHook); SendResult result; try { producer.start(); if (brokerName != null && queueId > -1) { MessageQueue messageQueue = new MessageQueue(topic, brokerName, queueId); result = producer.send(msg, messageQueue); } else { result = producer.send(msg); } } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } finally { producer.shutdown(); } System.out.printf("%-32s %-4s %-20s %s%n", "#Broker Name", "#QID", "#Send Result", "#MsgId" ); if (result != null) { System.out.printf("%-32s %-4s %-20s %s%n", result.getMessageQueue().getBrokerName(), result.getMessageQueue().getQueueId(), result.getSendStatus(), result.getMsgId() ); } else { System.out.printf("%-32s %-4s %-20s %s%n", "Unknown", "Unknown", "Failed", "None" ); } }
SendMessageCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { Message msg = null; String topic = commandLine.getOptionValue('t').trim(); String body = commandLine.getOptionValue('p').trim(); String tag = null; String keys = null; String brokerName = null; int queueId = -1; try { if (commandLine.hasOption('k')) { keys = commandLine.getOptionValue('k').trim(); } if (commandLine.hasOption('c')) { tag = commandLine.getOptionValue('c').trim(); } if (commandLine.hasOption('b')) { brokerName = commandLine.getOptionValue('b').trim(); } if (commandLine.hasOption('i')) { if (!commandLine.hasOption('b')) { System.out.print("Broker name must be set if the queue is chosen!"); return; } else { queueId = Integer.parseInt(commandLine.getOptionValue('i').trim()); } } msg = new Message(topic, tag, keys, body.getBytes("utf-8")); } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } DefaultMQProducer producer = this.createProducer(rpcHook); SendResult result; try { producer.start(); if (brokerName != null && queueId > -1) { MessageQueue messageQueue = new MessageQueue(topic, brokerName, queueId); result = producer.send(msg, messageQueue); } else { result = producer.send(msg); } } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } finally { producer.shutdown(); } System.out.printf("%-32s %-4s %-20s %s%n", "#Broker Name", "#QID", "#Send Result", "#MsgId" ); if (result != null) { System.out.printf("%-32s %-4s %-20s %s%n", result.getMessageQueue().getBrokerName(), result.getMessageQueue().getQueueId(), result.getSendStatus(), result.getMsgId() ); } else { System.out.printf("%-32s %-4s %-20s %s%n", "Unknown", "Unknown", "Failed", "None" ); } } }
SendMessageCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { Message msg = null; String topic = commandLine.getOptionValue('t').trim(); String body = commandLine.getOptionValue('p').trim(); String tag = null; String keys = null; String brokerName = null; int queueId = -1; try { if (commandLine.hasOption('k')) { keys = commandLine.getOptionValue('k').trim(); } if (commandLine.hasOption('c')) { tag = commandLine.getOptionValue('c').trim(); } if (commandLine.hasOption('b')) { brokerName = commandLine.getOptionValue('b').trim(); } if (commandLine.hasOption('i')) { if (!commandLine.hasOption('b')) { System.out.print("Broker name must be set if the queue is chosen!"); return; } else { queueId = Integer.parseInt(commandLine.getOptionValue('i').trim()); } } msg = new Message(topic, tag, keys, body.getBytes("utf-8")); } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } DefaultMQProducer producer = this.createProducer(rpcHook); SendResult result; try { producer.start(); if (brokerName != null && queueId > -1) { MessageQueue messageQueue = new MessageQueue(topic, brokerName, queueId); result = producer.send(msg, messageQueue); } else { result = producer.send(msg); } } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } finally { producer.shutdown(); } System.out.printf("%-32s %-4s %-20s %s%n", "#Broker Name", "#QID", "#Send Result", "#MsgId" ); if (result != null) { System.out.printf("%-32s %-4s %-20s %s%n", result.getMessageQueue().getBrokerName(), result.getMessageQueue().getQueueId(), result.getSendStatus(), result.getMsgId() ); } else { System.out.printf("%-32s %-4s %-20s %s%n", "Unknown", "Unknown", "Failed", "None" ); } }  }
SendMessageCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { Message msg = null; String topic = commandLine.getOptionValue('t').trim(); String body = commandLine.getOptionValue('p').trim(); String tag = null; String keys = null; String brokerName = null; int queueId = -1; try { if (commandLine.hasOption('k')) { keys = commandLine.getOptionValue('k').trim(); } if (commandLine.hasOption('c')) { tag = commandLine.getOptionValue('c').trim(); } if (commandLine.hasOption('b')) { brokerName = commandLine.getOptionValue('b').trim(); } if (commandLine.hasOption('i')) { if (!commandLine.hasOption('b')) { System.out.print("Broker name must be set if the queue is chosen!"); return; } else { queueId = Integer.parseInt(commandLine.getOptionValue('i').trim()); } } msg = new Message(topic, tag, keys, body.getBytes("utf-8")); } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } DefaultMQProducer producer = this.createProducer(rpcHook); SendResult result; try { producer.start(); if (brokerName != null && queueId > -1) { MessageQueue messageQueue = new MessageQueue(topic, brokerName, queueId); result = producer.send(msg, messageQueue); } else { result = producer.send(msg); } } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } finally { producer.shutdown(); } System.out.printf("%-32s %-4s %-20s %s%n", "#Broker Name", "#QID", "#Send Result", "#MsgId" ); if (result != null) { System.out.printf("%-32s %-4s %-20s %s%n", result.getMessageQueue().getBrokerName(), result.getMessageQueue().getQueueId(), result.getSendStatus(), result.getMsgId() ); } else { System.out.printf("%-32s %-4s %-20s %s%n", "Unknown", "Unknown", "Failed", "None" ); } }  @Override String commandName(); @Override String commandDesc(); @Override Options buildCommandlineOptions(Options options); @Override void execute(CommandLine commandLine, Options options, RPCHook rpcHook); }
SendMessageCommand implements SubCommand { @Override public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { Message msg = null; String topic = commandLine.getOptionValue('t').trim(); String body = commandLine.getOptionValue('p').trim(); String tag = null; String keys = null; String brokerName = null; int queueId = -1; try { if (commandLine.hasOption('k')) { keys = commandLine.getOptionValue('k').trim(); } if (commandLine.hasOption('c')) { tag = commandLine.getOptionValue('c').trim(); } if (commandLine.hasOption('b')) { brokerName = commandLine.getOptionValue('b').trim(); } if (commandLine.hasOption('i')) { if (!commandLine.hasOption('b')) { System.out.print("Broker name must be set if the queue is chosen!"); return; } else { queueId = Integer.parseInt(commandLine.getOptionValue('i').trim()); } } msg = new Message(topic, tag, keys, body.getBytes("utf-8")); } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } DefaultMQProducer producer = this.createProducer(rpcHook); SendResult result; try { producer.start(); if (brokerName != null && queueId > -1) { MessageQueue messageQueue = new MessageQueue(topic, brokerName, queueId); result = producer.send(msg, messageQueue); } else { result = producer.send(msg); } } catch (Exception e) { throw new RuntimeException(this.getClass().getSimpleName() + " command failed", e); } finally { producer.shutdown(); } System.out.printf("%-32s %-4s %-20s %s%n", "#Broker Name", "#QID", "#Send Result", "#MsgId" ); if (result != null) { System.out.printf("%-32s %-4s %-20s %s%n", result.getMessageQueue().getBrokerName(), result.getMessageQueue().getQueueId(), result.getSendStatus(), result.getMsgId() ); } else { System.out.printf("%-32s %-4s %-20s %s%n", "Unknown", "Unknown", "Failed", "None" ); } }  @Override String commandName(); @Override String commandDesc(); @Override Options buildCommandlineOptions(Options options); @Override void execute(CommandLine commandLine, Options options, RPCHook rpcHook);  }