View Javadoc

1   package org.apache.helix.recipes.rabbitmq;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import com.rabbitmq.client.Channel;
23  import com.rabbitmq.client.Connection;
24  import com.rabbitmq.client.ConnectionFactory;
25  
26  public class Emitter
27  {
28  
29    private static final String EXCHANGE_NAME = "topic_logs";
30  
31    public static void main(String[] args) throws Exception
32    {
33      if (args.length < 1)
34      {
35        System.err.println("USAGE: java Emitter rabbitmqServer (e.g. localhost) numberOfMessage (optional)");
36        System.exit(1);
37      }
38      
39      final String mqServer = args[0];  // "zzhang-ld";
40      int count = Integer.MAX_VALUE;
41      if (args.length > 1)
42      {
43        try
44        {
45          count = Integer.parseInt(args[1]);
46        } catch (Exception e) {
47          // TODO: handle exception
48        }
49      }
50      System.out.println("Sending " + count + " messages with random topic id");
51      
52  
53      ConnectionFactory factory = new ConnectionFactory();
54      factory.setHost(mqServer);
55      Connection connection = factory.newConnection();
56      Channel channel = connection.createChannel();
57  
58      channel.exchangeDeclare(EXCHANGE_NAME, "topic");
59  
60      for (int i = 0; i < count; i++)
61      {
62        int rand = ((int) (Math.random() * 10000) % SetupConsumerCluster.DEFAULT_PARTITION_NUMBER);
63        String routingKey = "topic_" + rand;
64        String message = "message_" + rand;
65  
66        channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
67        System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
68        
69        Thread.sleep(1000);
70      }
71      
72      connection.close();
73    }
74  
75  }