|
| 1 | +import java.io.IOException; |
| 2 | +import com.rabbitmq.client.ConnectionFactory; |
| 3 | +import com.rabbitmq.client.Connection; |
| 4 | +import com.rabbitmq.client.Channel; |
| 5 | + |
| 6 | +public class EmitLog { |
| 7 | + |
| 8 | + private static final String EXCHANGE_NAME = "logs"; |
| 9 | + |
| 10 | + public static void main(String[] argv) |
| 11 | + throws java.io.IOException { |
| 12 | + |
| 13 | + ConnectionFactory factory = new ConnectionFactory(); |
| 14 | + factory.setHost("localhost"); |
| 15 | + Connection connection = factory.newConnection(); |
| 16 | + Channel channel = connection.createChannel(); |
| 17 | + |
| 18 | + channel.exchangeDeclare(EXCHANGE_NAME, "fanout"); |
| 19 | + |
| 20 | + String message = getMessage(argv); |
| 21 | + |
| 22 | + channel.basicPublish( EXCHANGE_NAME, "", |
| 23 | + null, |
| 24 | + message.getBytes()); |
| 25 | + System.out.println(" [x] Sent '" + message + "'"); |
| 26 | + |
| 27 | + channel.close(); |
| 28 | + connection.close(); |
| 29 | + } |
| 30 | + |
| 31 | + private static String getMessage(String[] strings){ |
| 32 | + if (strings.length < 1) |
| 33 | + return "info: Hello World!"; |
| 34 | + return joinStrings(strings, " "); |
| 35 | + } |
| 36 | + |
| 37 | + private static String joinStrings(String[] strings, String delimiter) { |
| 38 | + int length = strings.length; |
| 39 | + if (length == 0) return ""; |
| 40 | + StringBuilder words = new StringBuilder(strings[0]); |
| 41 | + for (int i = 1; i < length; i++) { |
| 42 | + words.append(delimiter).append(strings[i]); |
| 43 | + } |
| 44 | + return words.toString(); |
| 45 | + } |
| 46 | + |
| 47 | +} |
0 commit comments