Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.chat; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; import java.awt.Dimension; import java.awt.GridLayout; import java.io.IOException; import java.util.concurrent.TimeoutException; import java.util.logging.Level; import java.util.logging.Logger; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; /** * * @author Eaiman */ public class UI extends javax.swing.JFrame { private String TANNEE_SHOSHI_MSG = ""; private String ALL_MSG = ""; private String TANNEE_NAHID_MSG = ""; private String NAHID_SHOSHI_MSG = ""; private String user = ""; private ConnectionFactory connectionFactory; private Connection connection; private Channel channel; /** * Creates new form UI */ public UI() { user = "NAHID"; initComponents(); initRabbitMQ(); jLabel1.setText("USER : " + user); if (user.equalsIgnoreCase("TANNEE")) { chatWindow.setTitleAt(1, "SHOSHI"); } else if (user.equalsIgnoreCase("NAHID")) { chatWindow.setTitleAt(2, "SHOSHI"); } } /** * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this code. The content of this method is always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents private void initComponents() { jLabel1 = new javax.swing.JLabel(); chatWindow = new javax.swing.JTabbedPane(); jPanel1 = new javax.swing.JPanel(); jTextField1 = new javax.swing.JTextField(); btnSend = new javax.swing.JButton(); jScrollPane3 = new javax.swing.JScrollPane(); jScrollPane1 = new javax.swing.JScrollPane(); jTextArea1 = new javax.swing.JTextArea(); jPanel5 = new javax.swing.JPanel(); jTextField5 = new javax.swing.JTextField(); btnSend4 = new javax.swing.JButton(); jScrollPane2 = new javax.swing.JScrollPane(); jScrollPane5 = new javax.swing.JScrollPane(); jTextArea5 = new javax.swing.JTextArea(); jPanel6 = new javax.swing.JPanel(); jTextField6 = new javax.swing.JTextField(); btnSend5 = new javax.swing.JButton(); jScrollPane7 = new javax.swing.JScrollPane(); jScrollPane6 = new javax.swing.JScrollPane(); jTextArea6 = new javax.swing.JTextArea(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jLabel1.setText("USER : SHOSHI"); chatWindow.setRequestFocusEnabled(false); jTextField1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField1ActionPerformed(evt); } }); btnSend.setText("SEND"); btnSend.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSendActionPerformed(evt); } }); jTextArea1.setColumns(20); jTextArea1.setRows(5); jTextArea1.setFocusable(false); jTextArea1.setRequestFocusEnabled(false); jTextArea1.setVerifyInputWhenFocusTarget(false); jScrollPane1.setViewportView(jTextArea1); jScrollPane3.setViewportView(jScrollPane1); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addComponent(jTextField1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnSend, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)) .addContainerGap())); jPanel1Layout .setVerticalGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel1Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField1).addComponent(btnSend, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE) .addContainerGap())); chatWindow.addTab("ALL", jPanel1); jTextField5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField5ActionPerformed(evt); } }); btnSend4.setText("SEND"); btnSend4.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSend4ActionPerformed(evt); } }); jTextArea5.setColumns(20); jTextArea5.setRows(5); jTextArea5.setFocusable(false); jTextArea5.setRequestFocusEnabled(false); jTextArea5.setVerifyInputWhenFocusTarget(false); jScrollPane5.setViewportView(jTextArea5); jScrollPane2.setViewportView(jScrollPane5); javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5); jPanel5.setLayout(jPanel5Layout); jPanel5Layout.setHorizontalGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addComponent(jTextField5) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnSend4, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)) .addContainerGap())); jPanel5Layout .setVerticalGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel5Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel5Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField5).addComponent(btnSend4, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE) .addContainerGap())); chatWindow.addTab("TANNEE", jPanel5); jTextField6.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jTextField6ActionPerformed(evt); } }); btnSend5.setText("SEND"); btnSend5.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { btnSend5ActionPerformed(evt); } }); jTextArea6.setColumns(20); jTextArea6.setRows(5); jTextArea6.setFocusable(false); jTextArea6.setRequestFocusEnabled(false); jTextArea6.setVerifyInputWhenFocusTarget(false); jScrollPane6.setViewportView(jTextArea6); jScrollPane7.setViewportView(jScrollPane6); javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6); jPanel6.setLayout(jPanel6Layout); jPanel6Layout.setHorizontalGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addComponent(jTextField6) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(btnSend5, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 653, Short.MAX_VALUE)) .addContainerGap())); jPanel6Layout .setVerticalGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel6Layout.createSequentialGroup().addContainerGap() .addGroup(jPanel6Layout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(jTextField6).addComponent(btnSend5, javax.swing.GroupLayout.DEFAULT_SIZE, 54, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jScrollPane7, javax.swing.GroupLayout.DEFAULT_SIZE, 242, Short.MAX_VALUE) .addContainerGap())); chatWindow.addTab("NAHID", jPanel6); jLabel2.setText("Host :"); jLabel3.setText("localhost"); jButton1.setText("Change Host"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(chatWindow) .addGroup(layout.createSequentialGroup().addGap(98, 98, 98) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(92, 92, 92) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGap(18, 18, 18).addComponent(jButton1).addContainerGap())); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup().addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(jLabel1).addComponent(jLabel2).addComponent(jLabel3) .addComponent(jButton1)) .addGap(10, 10, 10).addComponent(chatWindow))); pack(); }// </editor-fold>//GEN-END:initComponents private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed if (jTextField1.getText() == null || jTextField1.getText().trim().equals("")) { return; } String text = user + ": " + jTextField1.getText(); try { // channel.basicPublish("", Config.ALL, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_ALL, Config.E_ALL, text); channel.basicPublish(Config.E_ALL, Config.E_ALL, null, text.getBytes()); ALL_MSG += "ME: " + jTextField1.getText() + "\n"; jTextArea1.setText(ALL_MSG); jTextField1.setText(""); } catch (IOException ex) { Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnSendActionPerformed private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed if (jTextField1.getText() == null || jTextField1.getText().trim().equals("")) { return; } String text = user + ": " + jTextField1.getText(); try { // channel.basicPublish("", Config.ALL, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_ALL, Config.E_ALL, text); channel.basicPublish(Config.E_ALL, Config.E_ALL, null, text.getBytes()); ALL_MSG += "ME: " + jTextField1.getText() + "\n"; jTextArea1.setText(ALL_MSG); jTextField1.setText(""); } catch (IOException ex) { Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jTextField1ActionPerformed private void jTextField5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField5ActionPerformed if (jTextField5.getText() == null || jTextField5.getText().trim().equals("")) { return; } String text = user + ": " + jTextField5.getText(); try { if (user.equalsIgnoreCase("NAHID")) { // channel.basicPublish("", Config.TANNEE_NAHID, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, text); channel.basicPublish(Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, null, text.getBytes()); TANNEE_NAHID_MSG += "ME: " + jTextField5.getText() + "\n"; jTextArea5.setText(TANNEE_NAHID_MSG); jTextField5.setText(""); } else { // channel.basicPublish("", Config.TANNEE_SHOSHI, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI, text); channel.basicPublish(Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI, null, text.getBytes()); TANNEE_SHOSHI_MSG += "ME: " + jTextField5.getText() + "\n"; jTextArea5.setText(TANNEE_SHOSHI_MSG); jTextField5.setText(""); } } catch (IOException ex) { Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jTextField5ActionPerformed private void btnSend4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSend4ActionPerformed if (jTextField5.getText() == null || jTextField5.getText().trim().equals("")) { return; } String text = user + ": " + jTextField5.getText(); try { if (user.equalsIgnoreCase("NAHID")) { // channel.basicPublish("", Config.TANNEE_NAHID, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, text); channel.basicPublish(Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, null, text.getBytes()); TANNEE_NAHID_MSG += "ME: " + jTextField5.getText() + "\n"; jTextArea5.setText(TANNEE_NAHID_MSG); jTextField5.setText(""); } else { // channel.basicPublish("", Config.TANNEE_SHOSHI, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI, text); channel.basicPublish(Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI, null, text.getBytes()); TANNEE_SHOSHI_MSG += "ME: " + jTextField5.getText() + "\n"; jTextArea5.setText(TANNEE_SHOSHI_MSG); jTextField5.setText(""); } } catch (IOException ex) { Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnSend4ActionPerformed private void jTextField6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField6ActionPerformed if (jTextField6.getText() == null || jTextField6.getText().trim().equals("")) { return; } String text = user + ": " + jTextField6.getText(); try { if (user.equalsIgnoreCase("TANNEE")) { // channel.basicPublish("", Config.TANNEE_NAHID, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, text); channel.basicPublish(Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, null, text.getBytes()); TANNEE_NAHID_MSG += "ME: " + jTextField6.getText() + "\n"; jTextArea6.setText(TANNEE_NAHID_MSG); jTextField6.setText(""); } else { // channel.basicPublish("", Config.NAHID_SHOSHI, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI, text); channel.basicPublish(Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI, null, text.getBytes()); NAHID_SHOSHI_MSG += "ME: " + jTextField6.getText() + "\n"; jTextArea6.setText(NAHID_SHOSHI_MSG); jTextField6.setText(""); } } catch (IOException ex) { Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_jTextField6ActionPerformed private void btnSend5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSend5ActionPerformed if (jTextField6.getText() == null || jTextField6.getText().trim().equals("")) { return; } String text = user + ": " + jTextField6.getText(); try { if (user.equalsIgnoreCase("TANNEE")) { // channel.basicPublish("", Config.TANNEE_NAHID, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, text); channel.basicPublish(Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID, null, text.getBytes()); TANNEE_NAHID_MSG += "ME: " + jTextField6.getText() + "\n"; jTextArea6.setText(TANNEE_NAHID_MSG); jTextField6.setText(""); } else { // channel.basicPublish("", Config.NAHID_SHOSHI, null, text.getBytes()); // Sender.sendMsg(jLabel3.getText().trim(), Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI, text); channel.basicPublish(Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI, null, text.getBytes()); NAHID_SHOSHI_MSG += "ME: " + jTextField6.getText() + "\n"; jTextArea6.setText(NAHID_SHOSHI_MSG); jTextField6.setText(""); } } catch (IOException ex) { Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex); } }//GEN-LAST:event_btnSend5ActionPerformed private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed getHost(); }//GEN-LAST:event_jButton1ActionPerformed public void getHost() { JPanel p = new JPanel(new GridLayout(1, 1, 3, 3)); p.setPreferredSize(new Dimension(400, 50)); final JTextField t = new JTextField(""); // p.add(l); p.add(t); int option = JOptionPane.showConfirmDialog(null, p, "Change Host", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE); if (option == 0) { if (t.getText() != null && !t.getText().trim().equals("")) { jLabel3.setText(t.getText().trim()); initRabbitMQ(); } } } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(UI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { @Override public void run() { new UI().setVisible(true); } }); } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JButton btnSend; private javax.swing.JButton btnSend4; private javax.swing.JButton btnSend5; private javax.swing.JTabbedPane chatWindow; private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel5; private javax.swing.JPanel jPanel6; private javax.swing.JScrollPane jScrollPane1; private javax.swing.JScrollPane jScrollPane2; private javax.swing.JScrollPane jScrollPane3; private javax.swing.JScrollPane jScrollPane5; private javax.swing.JScrollPane jScrollPane6; private javax.swing.JScrollPane jScrollPane7; private javax.swing.JTextArea jTextArea1; private javax.swing.JTextArea jTextArea5; private javax.swing.JTextArea jTextArea6; private javax.swing.JTextField jTextField1; private javax.swing.JTextField jTextField5; private javax.swing.JTextField jTextField6; // End of variables declaration//GEN-END:variables private void initRabbitMQ() { try { if (connection != null && connection.isOpen()) { connection.close(); } if (channel != null && channel.isOpen()) { channel.close(); } connectionFactory = new ConnectionFactory(); connectionFactory.setHost(jLabel3.getText().trim()); connectionFactory.setUsername("shoshi"); connectionFactory.setPassword("shoshi"); // connectionFactory.setHost("192.168.0.101"); connectionFactory.setPort(5672); connection = connectionFactory.newConnection(); channel = connection.createChannel(); channel.exchangeDeclare(Config.E_ALL, "fanout", false); channel.exchangeDeclare(Config.E_TANNEE_NAHID, "fanout", false); channel.exchangeDeclare(Config.E_TANNEE_SHOSHI, "fanout", false); channel.exchangeDeclare(Config.E_NAHID_SHOSHI, "fanout", false); channel.queueDeclare(Config.Q_NAHID_ALL, false, false, false, null); channel.queueDeclare(Config.Q_NAHID_SHOSHI, false, false, false, null); channel.queueDeclare(Config.Q_NAHID_TANNEE, false, false, false, null); channel.queueDeclare(Config.Q_SHOSHI_ALL, false, false, false, null); channel.queueDeclare(Config.Q_SHOSHI_TANNEE, false, false, false, null); channel.queueDeclare(Config.Q_SHOSHI_NAHID, false, false, false, null); channel.queueDeclare(Config.Q_TANNEE_ALL, false, false, false, null); channel.queueDeclare(Config.Q_TANNEE_SHOSHI, false, false, false, null); channel.queueDeclare(Config.Q_TANNEE_NAHID, false, false, false, null); channel.queueBind(Config.Q_NAHID_ALL, Config.E_ALL, Config.E_ALL); channel.queueBind(Config.Q_NAHID_SHOSHI, Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI); channel.queueBind(Config.Q_NAHID_TANNEE, Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID); channel.queueBind(Config.Q_SHOSHI_ALL, Config.E_ALL, Config.E_ALL); channel.queueBind(Config.Q_SHOSHI_TANNEE, Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI); channel.queueBind(Config.Q_SHOSHI_NAHID, Config.E_NAHID_SHOSHI, Config.E_NAHID_SHOSHI); channel.queueBind(Config.Q_TANNEE_ALL, Config.E_ALL, Config.E_ALL); channel.queueBind(Config.Q_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI, Config.E_TANNEE_SHOSHI); channel.queueBind(Config.Q_TANNEE_NAHID, Config.E_TANNEE_NAHID, Config.E_TANNEE_NAHID); System.out.println("user: " + user); if (user.equalsIgnoreCase("NAHID")) { forNahid(); System.out.println("user: " + user); } else if (user.equalsIgnoreCase("TANNEE")) { forTannee(); System.out.println("user: " + user); } else if (user.equalsIgnoreCase("SHOSHI")) { forShoshi(); System.out.println("user: " + user); } } catch (IOException | TimeoutException ex) { // if(ex.toString().contains("UnknownHostException")){ // if(tryCounter++ < 2){ // jLabel3.setText(host); // initRabbitMQ(); // }else{ // tryCounter = 0; // jLabel3.setText("localhost"); // host = "localhost"; // initRabbitMQ(); // } // } Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex); } } private void forNahid() throws IOException { Consumer consumer1 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_ALL)) { ALL_MSG += message + "\n"; jTextArea1.setText(ALL_MSG); } } }; Consumer consumer2 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_NAHID_SHOSHI)) { NAHID_SHOSHI_MSG += message + "\n"; jTextArea6.setText(NAHID_SHOSHI_MSG); } } }; Consumer consumer3 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_NAHID)) { TANNEE_NAHID_MSG += message + "\n"; jTextArea5.setText(TANNEE_NAHID_MSG); } } }; channel.basicConsume(Config.Q_NAHID_ALL, true, consumer1); channel.basicConsume(Config.Q_NAHID_SHOSHI, true, consumer2); channel.basicConsume(Config.Q_NAHID_TANNEE, true, consumer3); } private void forTannee() throws IOException { Consumer consumer1 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_ALL)) { ALL_MSG += message + "\n"; jTextArea1.setText(ALL_MSG); } } }; Consumer consumer2 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_SHOSHI)) { TANNEE_SHOSHI_MSG += message + "\n"; jTextArea5.setText(TANNEE_SHOSHI_MSG); } } }; Consumer consumer3 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_NAHID)) { TANNEE_NAHID_MSG += message + "\n"; jTextArea6.setText(TANNEE_NAHID_MSG); } } }; channel.basicConsume(Config.Q_TANNEE_ALL, true, consumer1); channel.basicConsume(Config.Q_TANNEE_SHOSHI, true, consumer2); channel.basicConsume(Config.Q_TANNEE_NAHID, true, consumer3); } private void forShoshi() throws IOException { Consumer consumer1 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_ALL)) { ALL_MSG += message + "\n"; jTextArea1.setText(ALL_MSG); } } }; Consumer consumer2 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_NAHID_SHOSHI)) { NAHID_SHOSHI_MSG += message + "\n"; jTextArea6.setText(NAHID_SHOSHI_MSG); } } }; Consumer consumer3 = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println(" [x] Received '" + message + "'"); if (message.toLowerCase().startsWith(user.toLowerCase())) { return; } String routingKey = envelope.getRoutingKey(); System.out.println(" [x] routingKey '" + routingKey + "'"); if (routingKey.equalsIgnoreCase(Config.E_TANNEE_SHOSHI)) { TANNEE_SHOSHI_MSG += message + "\n"; jTextArea5.setText(TANNEE_SHOSHI_MSG); } } }; channel.basicConsume(Config.Q_SHOSHI_ALL, true, consumer1); channel.basicConsume(Config.Q_SHOSHI_TANNEE, true, consumer3); channel.basicConsume(Config.Q_SHOSHI_NAHID, true, consumer2); } }