从表数据库不断获取内容

时间:2017-04-21 06:43:17

标签: java mysql sql oracle postgresql

我有一个代码here

package testcode;

import java.sql.*;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class ProducerClear2 {

    public static String vardbserver;
    public static String vardbuser;
    public static String vardbpassword;
    public static String vardbname;

    public static void main(String[] args) {
        vardbserver = "TestDBtoMQ";
        vardbuser = "postgresql";
        vardbpassword = "admin";

        ConnectionFactory factory = null;
        javax.jms.Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        try {
            factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
            connection = factory.createConnection();
            connection.start();
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("TestQueue");
            producer = session.createProducer(destination);

            Class.forName("org.postgresql.Driver");
            System.out.println("----------------------------");

            try (Connection c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/" + vardbserver, vardbuser, vardbpassword);
                 PreparedStatement stmt = c.prepareStatement("SELECT * FROM MESSAGES where xmin::varchar::bigint > ? and xmin::varchar::bigint < ? ");
                 PreparedStatement max = c.prepareStatement("select max(xmin::varchar::bigint) as txid from messages")
            ) {
                c.setAutoCommit(false);

                Long previousTxId = 0L;
                Long nextTxId = 0L;

                while (true) {
                    stmt.clearParameters();

                    try (ResultSet rs = max.executeQuery()) {
                        if (rs.next()) {
                            nextTxId = rs.getLong(1);
                        }
                    }

                    stmt.setLong(1, previousTxId);
                    stmt.setLong(2, nextTxId + 1);
                    try (ResultSet rs = stmt.executeQuery()) {
                        while (rs.next()) {
                            String message = rs.getString("MESSAGE");
                            System.out.println("Message = " + message);
                            TextMessage mssg = session.createTextMessage(message);
                            System.out.println("Sent: " + mssg.getText());
                            producer.send(mssg);
                        }
                        previousTxId = nextTxId;
                    }
                    Thread.sleep(5000);
                }
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        } finally {
            if (session != null) {
                try {
                    session.close();
                } catch (JMSException ex) {
                    // ignore
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (JMSException ex) {
                    // ignore
                }
            }
        }
        System.out.println("----------------------------");
        System.out.println("Message sent successfully");
    }
}

基本上,该应用程序用于获取数据库表中的内容并将其发送到ActiveMQ。当表更新时,它将发送刚更新的内容(不发送已发送的过去)。但是这段代码只适用于PostgreSQL

然后我计划创建一个&#34; if&#34;功能。所以我可以使用另一个数据库来获取数据(Oracle和MySQL)。

xmin是否仍适用于Oracle和MySQL?所以我只需要更改服务器URL?或者我需要更改Oracle和MySQL的代码?

1 个答案:

答案 0 :(得分:0)

只需找到答案,使用限制,将每个限制行保存到文件中,并为每个日常文件使用日期....

              if(vardbtype.equals("MYSQL")){
                    Class.forName("com.mysql.jdbc.Driver");
                    System.out.println("----------------------------");
                    int limitrowmysql = 0;
                    LocalDate now = LocalDate.now();
                    Path path = FileSystems.getDefault().getPath("C:\\Users\\NN\\Documents\\Test\\RowMYSQL\\RowIDMYSQL_" + now.format(DateTimeFormatter.ISO_LOCAL_DATE) + ".txt");
                    if (Files.exists(path)) {
                        String latestRowIdFromFile  = Files.lines(path).max((e1, e2) -> {
                        if (((String)e1).isEmpty() || ((String)e2).isEmpty()) {
                            return -1;
                        }
                        return new Long(e1).compareTo(new Long(e2));
                        }).get(); // read latestRowId from file
                        if (latestRowIdFromFile != null && !latestRowIdFromFile.isEmpty()) {
                            limitrowmysql = Integer.valueOf(latestRowIdFromFile);
                        }
                    } else {
                        limitrowmysql = 0;
                    }
                    Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+ vardbserver, vardbuser, vardbpassword);
                    while(true) {
                        Statement stmts = c.createStatement();
                        int countrowmysql = 0;
                        String sql = ("SELECT * FROM "+ vardbname +" LIMIT "+ limitrowmysql +", 18446744073709551615");
                        ResultSet rss = stmts.executeQuery(sql);
                        while(rss.next()) {
                            String  message = rss.getString("MESSAGE");
                            System.out.println("Message = " + message);
                            TextMessage mssg = session.createTextMessage(message);
                            System.out.println("Sent: " + mssg.getText());
                            producer.send(mssg);
                            countrowmysql = countrowmysql + 1;
                        }

                        rss.close();
                        stmts.close();
                        Thread.sleep(batchperiod2);

                        limitrowmysql = limitrowmysql + countrowmysql;
                        Files.write(path, ("\n" + limitrowmysql).getBytes()); // write latestRowId to file
                    }
                }