Advertisement
Guest User

Untitled

a guest
Mar 15th, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. import com.rabbitmq.client.*;
  2. import com.rabbitmq.client.Connection;
  3. import java.io.IOException;
  4.  
  5. public class Reception {
  6. private final static String QUEUE_NAME = "MailQueue";
  7.  
  8. public static void main(String[] argv) throws Exception {
  9.  
  10. ConnectionFactory factory = new ConnectionFactory();
  11. factory.setHost("localhost");
  12. Connection connection = factory.newConnection();
  13. Channel channel = connection.createChannel();
  14. channel.queueDeclare(QUEUE_NAME, false, false, false, null);
  15.  
  16. Consumer consumer = new DefaultConsumer(channel) {
  17. @Override
  18. public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
  19. throws IOException {
  20. String message = new String(body, "UTF-8");
  21. processMessage(body);
  22. DataGet id = new DataGet();
  23. MailSender mail=new MailSender();
  24. mail.MailSending(id.DataGet(processMessage(body)));
  25. channel.basicAck(envelope.getDeliveryTag(), false);
  26. System.out.println(" [x] Received '" + message + "'");
  27. }
  28. };
  29. channel.basicConsume(QUEUE_NAME, false, consumer);
  30. }
  31.  
  32. public static int processMessage(byte[] delivery ){
  33. String id = new String(delivery);
  34. return Integer.parseInt(id);
  35. }
  36. }
  37.  
  38. public class DataGet {
  39. String from;
  40. String to;
  41. String subject;
  42. String body;
  43.  
  44.  
  45. public DataGet DataGet(int id) {
  46.  
  47. System.out.println("got id= " + id);
  48.  
  49. DataGet DataFromBD = new DataGet();
  50.  
  51. String Url = "jdbc:postgresql://127.0.0.1/newBD";
  52. String UserName = "postgres";
  53. String UserPassword = "postgres";
  54.  
  55. try {
  56. Connection DBConnection = DriverManager.getConnection(Url, UserName, UserPassword);
  57.  
  58. Statement statement = DBConnection.createStatement();
  59. ResultSet Result = statement.executeQuery(
  60. "select * from mailer.log where (id =id)"
  61. );
  62.  
  63. while (Result.next()) {
  64. DataFromBD.setTo(Result.getString("to"));
  65. DataFromBD.setFrom(Result.getString("from"));
  66. DataFromBD.setSubject(Result.getString("subject"));
  67. DataFromBD.setBody(Result.getString("body"));
  68. }
  69. Result.close();
  70.  
  71. } catch (SQLException e) {
  72. System.out.println(e);
  73. }
  74. System.out.println("got data: ");
  75. System.out.println("transmitting data to mailsender(using id= " + id + " )");
  76.  
  77. return DataFromBD;
  78. }
  79.  
  80. public void setTo(String to) {
  81. this.to = to;
  82. }
  83.  
  84. public void setFrom(String from) {
  85. this.from = from;
  86. }
  87.  
  88. public void setSubject(String subject) {
  89. this.subject = subject;
  90. }
  91.  
  92. public void setBody(String body) {
  93. this.body = body;
  94. }
  95.  
  96. public String getTo() {
  97. return to;
  98. }
  99.  
  100. public String getFrom() {
  101. return from;
  102. }
  103.  
  104. public String getSubject() {
  105. return subject;
  106. }
  107.  
  108. public String getBody() {
  109. return body;
  110. }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement