Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.rabbitmq.client.Channel;
- import com.rabbitmq.client.Connection;
- import com.rabbitmq.client.ConnectionFactory;
- import com.rabbitmq.client.DeliverCallback;
- import javafx.event.ActionEvent;
- import javafx.event.EventHandler;
- import javafx.geometry.Insets;
- import javafx.scene.Scene;
- import javafx.scene.control.Button;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.TextField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.layout.StackPane;
- import javafx.stage.Stage;
- import java.io.IOException;
- import java.util.concurrent.TimeoutException;
- public class UI {
- private final static String QUEUE_NAME = "hello";
- void run(Stage stage) throws IOException, TimeoutException {
- //Creating a GridPane container
- GridPane grid = new GridPane();
- grid.setPadding(new Insets(10, 10, 10, 10));
- grid.setVgap(5);
- grid.setHgap(5);
- //Defining the Name text field
- final TextField msg = new TextField();
- msg.setPromptText("Enter your first msg.");
- msg.setPrefColumnCount(10);
- msg.getText();
- GridPane.setConstraints(msg, 0, 0);
- grid.getChildren().add(msg);
- final TextArea response = new TextArea();
- // messenger.setOut(response);
- response.setPrefColumnCount(15);
- response.setPromptText("Response: ");
- GridPane.setConstraints(response, 0, 2);
- grid.getChildren().add(response);
- Button submit = new Button("Submit");
- GridPane.setConstraints(submit, 1, 0);
- grid.getChildren().add(submit);
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- submit.setOnAction(new EventHandler<ActionEvent>() {
- @Override
- public void handle(ActionEvent event) {
- String message = msg.getText();
- try {
- channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
- } catch (IOException e) {
- e.printStackTrace();
- }
- System.out.println(" [x] Sent '" + message + "'");
- //messenger.sendMessage(msg.getText());
- }
- });
- System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
- DeliverCallback deliverCallback = (consumerTag, delivery) -> {
- String message = new String(delivery.getBody(), "UTF-8");
- response.appendText(message + "\n");
- System.out.println(" [x] Received '" + message + "'");
- };
- channel.basicConsume(QUEUE_NAME, true, deliverCallback, consumerTag -> { });
- System.out.println("sDVSDvsADFVasdbvasb");
- Scene scene = new Scene(grid, 300, 500);
- stage.setScene(scene);
- stage.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement