forked from apache/rocketmq
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ISSUES apache#3048]add example of OnewayProducer and ScheduledMessage (
apache#3053) * fix the issue apache#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>
- Loading branch information
Showing
3 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
example/src/main/java/org/apache/rocketmq/example/schedule/ScheduledMessageConsumer.java
This file contains 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,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 changes: 41 additions & 0 deletions
41
example/src/main/java/org/apache/rocketmq/example/schedule/ScheduledMessageProducer.java
This file contains 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,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(); | ||
} | ||
|
||
} |
45 changes: 45 additions & 0 deletions
45
example/src/main/java/org/apache/rocketmq/example/simple/OnewayProducer.java
This file contains 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,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(); | ||
} | ||
} |