Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1.  
  2. import com.rabbitmq.client.Channel;
  3. import com.rabbitmq.client.Connection;
  4. import com.rabbitmq.client.ConnectionFactory;
  5. import com.rabbitmq.client.DeliverCallback;
  6. import javafx.event.ActionEvent;
  7. import javafx.event.EventHandler;
  8. import javafx.geometry.Insets;
  9. import javafx.scene.Scene;
  10. import javafx.scene.control.Button;
  11. import javafx.scene.control.TextArea;
  12. import javafx.scene.control.TextField;
  13. import javafx.scene.layout.GridPane;
  14. import javafx.scene.layout.StackPane;
  15. import javafx.stage.Stage;
  16.  
  17. import java.io.IOException;
  18. import java.util.concurrent.TimeoutException;
  19.  
  20. public class UI {
  21. private final static String QUEUE_NAME = "hello";
  22. void run(Stage stage) throws IOException, TimeoutException {
  23. //Creating a GridPane container
  24. GridPane grid = new GridPane();
  25. grid.setPadding(new Insets(10, 10, 10, 10));
  26. grid.setVgap(5);
  27. grid.setHgap(5);
  28. //Defining the Name text field
  29. final TextField msg = new TextField();
  30. msg.setPromptText("Enter your first msg.");
  31. msg.setPrefColumnCount(10);
  32. msg.getText();
  33. GridPane.setConstraints(msg, 0, 0);
  34. grid.getChildren().add(msg);
  35. final TextArea response = new TextArea();
  36. // messenger.setOut(response);
  37. response.setPrefColumnCount(15);
  38. response.setPromptText("Response: ");
  39. GridPane.setConstraints(response, 0, 2);
  40. grid.getChildren().add(response);
  41. Button submit = new Button("Submit");
  42. GridPane.setConstraints(submit, 1, 0);
  43. grid.getChildren().add(submit);
  44.  
  45.  
  46. ConnectionFactory factory = new ConnectionFactory();
  47. factory.setHost("localhost");
  48. Connection connection = factory.newConnection();
  49. Channel channel = connection.createChannel();
  50. channel.queueDeclare(QUEUE_NAME, false, false, false, null);
  51.  
  52. submit.setOnAction(new EventHandler<ActionEvent>() {
  53.  
  54. @Override
  55. public void handle(ActionEvent event) {
  56. String message = msg.getText();
  57. try {
  58. channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
  59. } catch (IOException e) {
  60. e.printStackTrace();
  61. }
  62. System.out.println(" [x] Sent '" + message + "'");
  63. //messenger.sendMessage(msg.getText());
  64. }
  65. });
  66.  
  67. System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
  68.  
  69. DeliverCallback deliverCallback = (consumerTag, delivery) -> {
  70. String message = new String(delivery.getBody(), "UTF-8");
  71. response.appendText(message + "\n");
  72. System.out.println(" [x] Received '" + message + "'");
  73. };
  74. channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
  75. System.out.println("sDVSDvsADFVasdbvasb");
  76. Scene scene = new Scene(grid, 300, 500);
  77. stage.setScene(scene);
  78. stage.show();
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement