-
Notifications
You must be signed in to change notification settings - Fork 12k
[ROCKETMQ-353] Add SendMessage Command #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1ae0693
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh f0e243c
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 1810be4
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 53dcd8d
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 4abfa4f
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh d576e38
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh bb446c4
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 11d40c2
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh ae5c41e
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 89dbf04
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh bc1c880
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 48d93e8
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 962dfbf
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 844f0a9
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 5567377
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh c1f503c
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh b7fb571
add sendMessage command
lindzh ccbc4b4
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh bbf1733
Merge branch 'develop' into tools_sendmsg
lindzh 746b275
fix command parameter comment
lindzh 171799e
Merge branch 'develop' of github.com:apache/incubator-rocketmq into d…
lindzh 7cb2a2e
Merge branch 'develop' into tools_sendmsg
lindzh ea5f15b
add tools send msg test case
lindzh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
tools/src/main/java/org/apache/rocketmq/tools/command/message/SendMessageCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.rocketmq.tools.command.message; | ||
|
|
||
| import org.apache.commons.cli.CommandLine; | ||
| import org.apache.commons.cli.Option; | ||
| import org.apache.commons.cli.Options; | ||
| import org.apache.rocketmq.client.producer.DefaultMQProducer; | ||
| import org.apache.rocketmq.client.producer.SendResult; | ||
| import org.apache.rocketmq.common.message.Message; | ||
| import org.apache.rocketmq.common.message.MessageQueue; | ||
| import org.apache.rocketmq.remoting.RPCHook; | ||
| import org.apache.rocketmq.tools.command.SubCommand; | ||
| import org.apache.rocketmq.tools.command.SubCommandException; | ||
|
|
||
| public class SendMessageCommand implements SubCommand { | ||
|
|
||
| private DefaultMQProducer producer; | ||
|
|
||
| @Override | ||
| public String commandName() { | ||
| return "sendMessage"; | ||
| } | ||
|
|
||
| @Override | ||
| public String commandDesc() { | ||
| return "Send Message to a topic"; | ||
| } | ||
|
|
||
| @Override | ||
| public Options buildCommandlineOptions(Options options) { | ||
| Option opt = new Option("t", "topic", true, "topic name"); | ||
| opt.setRequired(true); | ||
| options.addOption(opt); | ||
|
|
||
| opt = new Option("b", "body", true, "utf-8 string format of the message body"); | ||
| opt.setRequired(true); | ||
| options.addOption(opt); | ||
|
|
||
| opt = new Option("k", "key", true, "message keys"); | ||
| opt.setRequired(false); | ||
| options.addOption(opt); | ||
|
|
||
| opt = new Option("g", "tags", true, "message tags"); | ||
| opt.setRequired(false); | ||
| options.addOption(opt); | ||
|
|
||
| opt = new Option("o", "qbroker", true, "send message to which broker"); | ||
| opt.setRequired(false); | ||
| options.addOption(opt); | ||
|
|
||
| opt = new Option("i", "qid", true, "send message to which queue id"); | ||
| opt.setRequired(false); | ||
| options.addOption(opt); | ||
|
|
||
| return options; | ||
| } | ||
|
|
||
| public DefaultMQProducer createProducer(RPCHook rpcHook) { | ||
| if (this.producer != null) { | ||
| return producer; | ||
| } else { | ||
| producer = new DefaultMQProducer(rpcHook); | ||
| producer.setProducerGroup(Long.toString(System.currentTimeMillis())); | ||
| return producer; | ||
| } | ||
| } | ||
|
|
||
| @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('b').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('g')) { | ||
| tag = commandLine.getOptionValue('g').trim(); | ||
| } | ||
| if (commandLine.hasOption('q')) { | ||
| brokerName = commandLine.getOptionValue('q').trim(); | ||
| } | ||
| if (commandLine.hasOption('i')) { | ||
| 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 = null; | ||
| 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" | ||
| ); | ||
| } | ||
| } | ||
| } |
136 changes: 136 additions & 0 deletions
136
tools/src/test/java/org/apache/rocketmq/tools/command/broker/SendMessageCommandTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.rocketmq.tools.command.broker; | ||
|
|
||
| import org.apache.commons.cli.CommandLine; | ||
| import org.apache.commons.cli.Options; | ||
| import org.apache.commons.cli.PosixParser; | ||
| import org.apache.rocketmq.client.exception.MQBrokerException; | ||
| import org.apache.rocketmq.client.exception.MQClientException; | ||
| import org.apache.rocketmq.client.producer.DefaultMQProducer; | ||
| import org.apache.rocketmq.client.producer.SendResult; | ||
| import org.apache.rocketmq.client.producer.SendStatus; | ||
| import org.apache.rocketmq.common.MixAll; | ||
| import org.apache.rocketmq.common.message.Message; | ||
| import org.apache.rocketmq.common.message.MessageQueue; | ||
| import org.apache.rocketmq.remoting.exception.RemotingException; | ||
| import org.apache.rocketmq.srvutil.ServerUtil; | ||
| import org.apache.rocketmq.tools.command.MQAdminStartup; | ||
| import org.apache.rocketmq.tools.command.SubCommandException; | ||
| import org.apache.rocketmq.tools.command.message.SendMessageCommand; | ||
| import org.junit.After; | ||
| import org.junit.Assert; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.Mockito; | ||
| import org.mockito.invocation.InvocationOnMock; | ||
| import org.mockito.stubbing.Answer; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.io.PrintStream; | ||
| import java.lang.reflect.Field; | ||
| import java.net.URL; | ||
|
|
||
| import static org.mockito.Mockito.doAnswer; | ||
|
|
||
| @RunWith(org.mockito.junit.MockitoJUnitRunner.class) | ||
| public class SendMessageCommandTest { | ||
|
|
||
| private SendMessageCommand sendMessageCommand = new SendMessageCommand(); | ||
| @Mock | ||
| private DefaultMQProducer defaultMQProducer; | ||
|
|
||
| private SendResult sendResult; | ||
|
|
||
| private SendResult sendResultByQueue; | ||
|
|
||
| @Before | ||
| public void init() throws MQClientException, SubCommandException, RemotingException, InterruptedException, MQBrokerException, NoSuchFieldException, IllegalAccessException { | ||
| sendResult = new SendResult(); | ||
| sendResult.setMessageQueue(new MessageQueue()); | ||
| sendResult.getMessageQueue().setBrokerName("broker1"); | ||
| sendResult.getMessageQueue().setQueueId(1); | ||
| sendResult.setSendStatus(SendStatus.SEND_OK); | ||
| sendResult.setMsgId("fgwejigherughwueyutyu4t4343t43"); | ||
|
|
||
| sendResultByQueue = sendResult; | ||
|
|
||
|
|
||
| doAnswer(new Answer() { | ||
| @Override | ||
| public Object answer(InvocationOnMock invocationOnMock) throws Throwable { | ||
| return sendResult; | ||
| } | ||
| }).when(defaultMQProducer).send(Mockito.any(Message.class)); | ||
|
|
||
| doAnswer(new Answer() { | ||
| @Override | ||
| public Object answer(InvocationOnMock invocationOnMock) throws Throwable { | ||
| return sendResultByQueue; | ||
| } | ||
| }).when(defaultMQProducer).send(Mockito.any(Message.class),Mockito.any(MessageQueue.class)); | ||
|
|
||
| Field producerField = SendMessageCommand.class.getDeclaredField("producer"); | ||
| producerField.setAccessible(true); | ||
| producerField.set(sendMessageCommand,defaultMQProducer); | ||
| } | ||
|
|
||
| @After | ||
| public void terminate() { | ||
|
|
||
| } | ||
|
|
||
| @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","-b 'send message test'","-g 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","-b 'send message test'","-g tagA","-k order-16546745756","-o 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")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSendMessageCommandAdd() { | ||
| URL resource = SendMessageCommandTest.class.getClassLoader().getResource("conf/logback_tools.xml"); | ||
| String file = resource.getFile(); | ||
| File file1 = new File(file); | ||
| String path = file1.getParentFile().getParentFile().getPath(); | ||
| System.setProperty(MixAll.ROCKETMQ_HOME_PROPERTY, path); | ||
| PrintStream out = System.out; | ||
| ByteArrayOutputStream bos = new ByteArrayOutputStream(); | ||
| System.setOut(new PrintStream(bos)); | ||
| MQAdminStartup.main(new String[]{"sendMessage"}); | ||
| System.setOut(out); | ||
| String s = new String(bos.toByteArray()); | ||
| Assert.assertTrue(s.contains("utf-8 string format of the message body")); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
awesome