Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.rabbitmq.client.*;
- import com.rabbitmq.client.Connection;
- import java.io.IOException;
- public class Reception {
- private final static String QUEUE_NAME = "MailQueue";
- public static void main(String[] argv) throws Exception {
- ConnectionFactory factory = new ConnectionFactory();
- factory.setHost("localhost");
- Connection connection = factory.newConnection();
- Channel channel = connection.createChannel();
- channel.queueDeclare(QUEUE_NAME, false, false, false, null);
- Consumer consumer = 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");
- processMessage(body);
- DataGet id = new DataGet();
- MailSender mail=new MailSender();
- mail.MailSending(id.DataGet(processMessage(body)));
- channel.basicAck(envelope.getDeliveryTag(), false);
- System.out.println(" [x] Received '" + message + "'");
- }
- };
- channel.basicConsume(QUEUE_NAME, false, consumer);
- }
- public static int processMessage(byte[] delivery ){
- String id = new String(delivery);
- return Integer.parseInt(id);
- }
- }
- public class DataGet {
- String from;
- String to;
- String subject;
- String body;
- public DataGet DataGet(int id) {
- System.out.println("got id= " + id);
- DataGet DataFromBD = new DataGet();
- String Url = "jdbc:postgresql://127.0.0.1/newBD";
- String UserName = "postgres";
- String UserPassword = "postgres";
- try {
- Connection DBConnection = DriverManager.getConnection(Url, UserName, UserPassword);
- Statement statement = DBConnection.createStatement();
- ResultSet Result = statement.executeQuery(
- "select * from mailer.log where (id =id)"
- );
- while (Result.next()) {
- DataFromBD.setTo(Result.getString("to"));
- DataFromBD.setFrom(Result.getString("from"));
- DataFromBD.setSubject(Result.getString("subject"));
- DataFromBD.setBody(Result.getString("body"));
- }
- Result.close();
- } catch (SQLException e) {
- System.out.println(e);
- }
- System.out.println("got data: ");
- System.out.println("transmitting data to mailsender(using id= " + id + " )");
- return DataFromBD;
- }
- public void setTo(String to) {
- this.to = to;
- }
- public void setFrom(String from) {
- this.from = from;
- }
- public void setSubject(String subject) {
- this.subject = subject;
- }
- public void setBody(String body) {
- this.body = body;
- }
- public String getTo() {
- return to;
- }
- public String getFrom() {
- return from;
- }
- public String getSubject() {
- return subject;
- }
- public String getBody() {
- return body;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement