OmniSciDB  04ee39c94c
FlavorPicker.java
Go to the documentation of this file.
1 package flavors;
2 
3 // Swing/AWT Interface classes
4 // Kafka Producer-specific classes
5 import org.apache.kafka.clients.producer.KafkaProducer;
6 import org.apache.kafka.clients.producer.Producer;
7 import org.apache.kafka.clients.producer.ProducerRecord;
8 
9 import java.awt.EventQueue;
10 import java.awt.event.ActionEvent;
11 import java.awt.event.ActionListener;
12 // Generic Java properties object
13 import java.util.Properties;
14 
15 import javax.swing.JButton;
16 import javax.swing.JFrame;
17 import javax.swing.JLabel;
18 
19 public class FlavorPicker {
20  private JFrame frmFlavors;
21  private Producer<String, String> producer;
25  public static void main(String[] args) {
26  EventQueue.invokeLater(new Runnable() {
27  public void run() {
28  try {
29  FlavorPicker window = new FlavorPicker(args);
30  window.frmFlavors.setVisible(true);
31  } catch (Exception e) {
32  e.printStackTrace();
33  }
34  }
35  });
36  }
37 
41  public FlavorPicker(String[] args) {
42  initialize(args);
43  }
44 
48  private void initialize(String[] args) {
49  frmFlavors = new JFrame();
50  frmFlavors.setTitle("Flavors");
51  frmFlavors.setBounds(100, 100, 408, 177);
52  frmFlavors.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
53  frmFlavors.getContentPane().setLayout(null);
54 
55  final JLabel lbl_yourPick = new JLabel("You picked nothing.");
56  lbl_yourPick.setBounds(130, 85, 171, 15);
57  frmFlavors.getContentPane().add(lbl_yourPick);
58 
59  JButton button = new JButton("Strawberry");
60  button.addActionListener(new ActionListener() {
61  public void actionPerformed(ActionEvent arg0) {
62  lbl_yourPick.setText("You picked strawberry.");
63  pick(args, 1);
64  }
65  });
66  button.setBounds(141, 12, 114, 25);
67  frmFlavors.getContentPane().add(button);
68 
69  JButton btnVanilla = new JButton("Vanilla");
70  btnVanilla.addActionListener(new ActionListener() {
71  public void actionPerformed(ActionEvent e) {
72  lbl_yourPick.setText("You picked vanilla.");
73  pick(args, 2);
74  }
75  });
76  btnVanilla.setBounds(278, 12, 82, 25);
77  frmFlavors.getContentPane().add(btnVanilla);
78 
79  JButton btnChocolate = new JButton("Chocolate");
80  btnChocolate.addActionListener(new ActionListener() {
81  public void actionPerformed(ActionEvent e) {
82  lbl_yourPick.setText("You picked chocolate.");
83  pick(args, 0);
84  }
85  });
86 
87  btnChocolate.setBounds(12, 12, 105, 25);
88  frmFlavors.getContentPane().add(btnChocolate);
89  }
90  public void pick(String[] args, int x) {
91  String topicName = args[0].toString();
92  String[] value = {"chocolate", "strawberry", "vanilla"};
93 
94  // Set the producer configuration properties.
95  Properties props = new Properties();
96  props.put("bootstrap.servers", "localhost:9097"); // 9097 to avoid Immerse:6273
97  props.put("acks", "all");
98  props.put("retries", 0);
99  props.put("batch.size", 100);
100  props.put("linger.ms", 1);
101  props.put("buffer.memory", 33554432);
102  props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
103  props.put(
104  "value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
105 
106  // Instantiate a producerSampleJDBC
107  producer = new KafkaProducer<String, String>(props);
108 
109  // Send a 1000 record stream to the Kafka Broker
110  for (int y = 0; y < 1000; y++) {
111  producer.send(new ProducerRecord<String, String>(topicName, value[x]));
112  }
113  }
114 } // End FlavorPicker.java
void initialize(String[] args)
void pick(String[] args, int x)
static void main(String[] args)
Producer< String, String > producer
static bool run
FlavorPicker(String[] args)