Oracle:向JMS发送最新的插入/更新

时间:2018-04-23 16:01:15

标签: oracle activemq mq oracle-aq

我有一个Oracle表的插入/更新触发器。 有没有办法将受影响的行(所有列)的详细信息作为消息发送给JMS? 我可以写一个Java程序,' loadjava'那个并从触发器调用。 这会影响性能吗?

是否有任何本地方式来实现这一目标?

2 个答案:

答案 0 :(得分:1)

确实存在本地方式:使用PL / SQL中的AQ JMS,请参阅https://docs.oracle.com/database/121/ADQUE/jm_exmpl.htm#ADQUE1600。简而言之,您创建一个具有JMS有效负载类型的AQ队列;然后你可以从触发器发布带有PL / SQL的消息。外部Java客户端可以连接到数据库并使用JMS读取消息。

我不知道对Java的调用会对性能产生多大影响,但我会尽量避免它。这是一个不错的主意,但它从来没有真正流行起来,所以它仍然是一个边缘情况,至少在早期总有问题。另一方面,PL / SQL可以工作。

如果需要将数据发送到另一个消息队列产品(标记activemq和mq),您可以使用Java读取消息并转发它们。它增加了一个额外的步骤,但它很简单。

答案 1 :(得分:0)

loadjava有很多问题而且如果加载了很多类并且很多业务不稳定,请看看Calling Java from Oracle, PLSQL causing oracle.aurora.vm.ReadOnlyObjectException

据我所知,Oracle AQ不是免费的。

我尝试了许多可能性后,通过仅使用loadjava创建加载到oracle的1个类来实现相同的需求,loadjava被触发器调用为过程并且负责调用具有所有必需参数的外部java程序并记录外部进程输出到表格,如下所示。

我已经将文本消息编码到BASE64,因为我使用了JSON格式,一些特殊字符可能导致问题作为外部java程序的参数。

如果我需要向外部程序发送许多参数,我已在发送的参数字符串中使用"#*#jms_separator#*#"作为分隔符来解析内容。

ShellExecutor.shellExec的整个持续时间大约为500毫秒,从1年开始运行没有任何问题。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Arrays;
import java.util.concurrent.FutureTask;

import sun.misc.BASE64Encoder;

public class ShellExecutor {

    static {
        System.setProperty("file.encoding", "UTF-8");
    }

    private static final String INSERT_LOGS_SQL = "INSERT INTO JMS_LOG (TEXT_LOG) VALUES (?) ";
    private static final String DEFAULT_CONNECTION = "jdbc:default:connection:";

    public static String SQLshellExec(String command) throws Exception {
        long start = System.currentTimeMillis();
        StringBuffer result = new StringBuffer();
        ShellExecutor worker = new ShellExecutor();
        try {
            worker.shellExec(command, result);
        } finally {
            result.append("exe duration : " + (System.currentTimeMillis() - start + "\n"));
            Connection dbConnection = null;
            PreparedStatement logsStatement = null;
            try {
                dbConnection = DriverManager.getConnection(DEFAULT_CONNECTION);
                logsStatement = dbConnection.prepareStatement(INSERT_LOGS_SQL);
                logsStatement.clearParameters();
                Clob clob = dbConnection.createClob();
                clob.setString(1, result.toString());
                logsStatement.setClob(1, clob);
                logsStatement.executeUpdate();
            } finally {
                if (logsStatement != null) {
                    try {
                        logsStatement.close();
                    } catch (Exception e) {
                    }
                }
            }
        }
        return result.substring(result.length() - 3090);
    }

    public void shellExec(String command, StringBuffer result) throws Exception {
        Process process = null;
        int exit = -10;
        try {
            InputStream stdout = null;
            String[] params = command.split("#*#jms_separator#*#");
            BASE64Encoder benc = new BASE64Encoder();
            for (int i = 0; i < params.length; i++) {
                if (params[i].contains("{") || params[i].contains("}") || params[i].contains("<")
                        || params[i].contains("/>")) {
                    params[i] = benc.encodeBuffer(params[i].getBytes("UTF-8"));
                }
            }
            result.append("Using separator : " + "#*#jms_separator#*#").append("\n")
                    .append("Calling : " + Arrays.toString(params)).append("\n");
            ProcessBuilder pb = new ProcessBuilder(params);
            pb.redirectErrorStream(true);
            process = pb.start();

            stdout = process.getInputStream();

            LogStreamReader lsr = new LogStreamReader(stdout, result);
            FutureTask<String> stdoutFuture = new FutureTask<String>(lsr, null);
            Thread thread = new Thread(stdoutFuture, "LogStreamReader");
            thread.start();

            try {
                exit = process.waitFor();
            } catch (InterruptedException e) {
                try {
                    exit = process.waitFor();
                } catch (Exception e1) {
                }
            }
            stdoutFuture.get();
            result.append("\n").append("exit code :").append(exit).append("\n");
            if (exit != 0) {
                throw new RuntimeException(result.toString());
            }
        } catch (Exception e) {
            result.append("\nException(").append(e.toString()).append("):").append(e.getCause()).append("\n\n");
            e.printStackTrace(System.err);
            throw e;
        } finally {
            if (process != null) {
                process.destroy();
            }
        }
    }
}

class LogStreamReader implements Runnable {

    private BufferedReader reader;
    private StringBuffer result;

    public LogStreamReader(InputStream is, StringBuffer result) {
        this.reader = new BufferedReader(new InputStreamReader(is));
        this.result = result;
    }

    public void run() {
        try {
            String line = null;
            while ((line = reader.readLine()) != null) {
                result.append(line).append("\n");
            }
        } catch (Exception e) {
            result.append("\nException(").append(e.toString()).append("):").append(e.getCause()).append("\n\n");
            e.printStackTrace(System.err);
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
    }
}

外部Java程序的类打包为可执行文件,包含所有需要的库,一个简单的JMS发送者:

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;

import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.commons.codec.binary.Base64;
import org.json.JSONObject;

import progress.message.jclient.ConnectionFactory;
import progress.message.jimpl.Connection;

public class JMSSender {
    private static SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS");

    public static void main(String[] args) throws Throwable {
        doSend(args[0]);
    }

    public static void doSend(String text)
            throws Throwable {
        if (Base64.isBase64(text)) {
            text = new String(Base64.decodeBase64(text));
        }
        String content = "\n\nsending message :" + text;
        Connection con = null;
        Session session = null;
        try {
            ConnectionFactory cf = new ConnectionFactory();
            session = con.createSession(false, Session.AUTO_ACKNOWLEDGE);
            Destination dest =  session.createTopic(destination) ;
            MessageProducer producer = session.createProducer(dest);
            con.start();
            JSONObject json = new JSONObject();
            json.put("content", text);
            json.put("date", sdf.format(new Date()));

            TextMessage tm = session.createTextMessage(json.toString());
            producer.send(tm);
            content += " \n\n" + "sent message :" + json.toString();
        } catch (Throwable e) {
            content += " \n\n" + e.toString() + " \n\n" + Arrays.toString(e.getStackTrace());
            if (e.getCause() != null) {
                content += " \n\nCause : " + e.getCause().toString() + " \n\n"
                        + Arrays.toString(e.getCause().getStackTrace());
            }
            e.printStackTrace(System.err);
            throw e;
        } finally {
            write("steps on sending message : " + content);
            if (session != null) {
                try {
                    session.commit();
                    session.close();
                } catch (Exception e) {
                }
                session = null;
            }
            if (con != null) {
                try {
                    con.stop();
                    con.close();
                } catch (Exception e) {
                }
            }
        }
    }

    private static void write(String log) {
        try {
            if (System.out != null) {
                System.out.println(log);
            }
        } catch (Exception e2) {
        }
    }
}