mirror of
https://github.com/apache/rocketmq.git
synced 2026-08-28 20:09:14 +08:00
* fix the issue #2994 add the example for OnewayProducer and ScheduledMessageConsumer. remove a unused line and add notes. * rollback * change code style * modify the code style Co-authored-by: zoupeinie <zoupeinie@bytedance.com>
This commit is contained in:
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.example.schedule;
|
||||
|
||||
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
|
||||
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
|
||||
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
|
||||
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
|
||||
import org.apache.rocketmq.common.message.MessageExt;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ScheduledMessageConsumer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Instantiate message consumer
|
||||
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("ExampleConsumer");
|
||||
// Subscribe topics
|
||||
consumer.subscribe("TestTopic", "*");
|
||||
// Register message listener
|
||||
consumer.registerMessageListener(new MessageListenerConcurrently() {
|
||||
@Override
|
||||
public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> messages, ConsumeConcurrentlyContext context) {
|
||||
for (MessageExt message : messages) {
|
||||
// Print approximate delay time period
|
||||
System.out.printf("Receive message[msgId=%s %d ms later]\n", message.getMsgId(),
|
||||
System.currentTimeMillis() - message.getStoreTimestamp());
|
||||
}
|
||||
return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
|
||||
}
|
||||
});
|
||||
// Launch consumer
|
||||
consumer.start();
|
||||
//info:to see the time effect, run the consumer first , it will wait for the msg
|
||||
//then start the producer
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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.example.schedule;
|
||||
|
||||
import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
|
||||
public class ScheduledMessageProducer {
|
||||
public static void main(String[] args) throws Exception {
|
||||
// Instantiate a producer to send scheduled messages
|
||||
DefaultMQProducer producer = new DefaultMQProducer("ExampleProducerGroup");
|
||||
// Launch producer
|
||||
producer.start();
|
||||
int totalMessagesToSend = 100;
|
||||
for (int i = 0; i < totalMessagesToSend; i++) {
|
||||
Message message = new Message("TestTopic", ("Hello scheduled message " + i).getBytes());
|
||||
// This message will be delivered to consumer 10 seconds later.
|
||||
message.setDelayTimeLevel(3);
|
||||
// Send the message
|
||||
producer.send(message);
|
||||
}
|
||||
|
||||
// Shutdown producer after use.
|
||||
producer.shutdown();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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.example.simple;
|
||||
|
||||
import org.apache.rocketmq.client.producer.DefaultMQProducer;
|
||||
import org.apache.rocketmq.common.message.Message;
|
||||
import org.apache.rocketmq.remoting.common.RemotingHelper;
|
||||
|
||||
public class OnewayProducer {
|
||||
public static void main(String[] args) throws Exception {
|
||||
//Instantiate with a producer group name.
|
||||
DefaultMQProducer producer = new DefaultMQProducer("please_rename_unique_group_name");
|
||||
// Specify name server addresses.
|
||||
producer.setNamesrvAddr("localhost:9876");
|
||||
//Launch the instance.
|
||||
producer.start();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
//Create a message instance, specifying topic, tag and message body.
|
||||
Message msg = new Message("TopicTest" /* Topic */,
|
||||
"TagA" /* Tag */,
|
||||
("Hello RocketMQ " +
|
||||
i).getBytes(RemotingHelper.DEFAULT_CHARSET) /* Message body */
|
||||
);
|
||||
//Call send message to deliver message to one of brokers.
|
||||
producer.sendOneway(msg);
|
||||
}
|
||||
//Wait for sending to complete
|
||||
Thread.sleep(5000);
|
||||
producer.shutdown();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user