1+ /*
2+ * Licensed to the Apache Software Foundation (ASF) under one or more
3+ * contributor license agreements. See the NOTICE file distributed with
4+ * this work for additional information regarding copyright ownership.
5+ * The ASF licenses this file to You under the Apache License, Version 2.0
6+ * (the "License"); you may not use this file except in compliance with
7+ * the License. You may obtain a copy of the License at
8+ *
9+ * http://www.apache.org/licenses/LICENSE-2.0
10+ *
11+ * Unless required by applicable law or agreed to in writing, software
12+ * distributed under the License is distributed on an "AS IS" BASIS,
13+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+ * See the License for the specific language governing permissions and
15+ * limitations under the License.
16+ */
17+ using System ;
18+ using System . Collections . Generic ;
19+ using System . Threading ;
20+ using System . Threading . Tasks ;
21+ using Microsoft . Extensions . Logging ;
22+ using Org . Apache . Rocketmq ;
23+
24+ namespace examples
25+ {
26+ public class PushConsumerExample
27+ {
28+ private static readonly ILogger Logger = MqLogManager . CreateLogger ( typeof ( PushConsumerExample ) . FullName ) ;
29+
30+ private static readonly string AccessKey = Environment . GetEnvironmentVariable ( "ROCKETMQ_ACCESS_KEY" ) ;
31+ private static readonly string SecretKey = Environment . GetEnvironmentVariable ( "ROCKETMQ_SECRET_KEY" ) ;
32+ private static readonly string Endpoint = Environment . GetEnvironmentVariable ( "ROCKETMQ_ENDPOINT" ) ;
33+
34+ internal static async Task QuickStart ( )
35+ {
36+ // Enable the switch if you use .NET Core 3.1 and want to disable TLS/SSL.
37+ // AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
38+
39+ // Credential provider is optional for client configuration.
40+ var credentialsProvider = new StaticSessionCredentialsProvider ( AccessKey , SecretKey ) ;
41+ var clientConfig = new ClientConfig . Builder ( )
42+ . SetEndpoints ( Endpoint )
43+ . SetCredentialsProvider ( credentialsProvider )
44+ . Build ( ) ;
45+
46+ // Add your subscriptions.
47+ const string consumerGroup = "yourConsumerGroup" ;
48+ const string topic = "yourTopic" ;
49+ var subscription = new Dictionary < string , FilterExpression >
50+ { { topic , new FilterExpression ( "*" ) } } ;
51+
52+ var pushConsumer = await new PushConsumer . Builder ( )
53+ . SetClientConfig ( clientConfig )
54+ . SetConsumerGroup ( consumerGroup )
55+ . SetSubscriptionExpression ( subscription )
56+ . SetMessageListener ( new CustomMessageListener ( ) )
57+ . Build ( ) ;
58+
59+ Thread . Sleep ( Timeout . Infinite ) ;
60+
61+ // Close the push consumer if you don't need it anymore.
62+ // await pushConsumer.DisposeAsync();
63+ }
64+
65+ private class CustomMessageListener : IMessageListener
66+ {
67+ public ConsumeResult Consume ( MessageView messageView )
68+ {
69+ // Handle the received message and return consume result.
70+ Logger . LogInformation ( $ "Consume message={ messageView } ") ;
71+ return ConsumeResult . SUCCESS ;
72+ }
73+ }
74+ }
75+ }
0 commit comments