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

Oracle:向JMS发送最新插入/更新,oracle,activemq,mq,oracle-aq,Oracle,Activemq,Mq,Oracle Aq,我有一个Oracle表的insert/update触发器。 是否有方法将受影响行(所有列)的详细信息作为消息发送给JMS? 我可以编写一个Java程序“loadjava”,并从触发器调用它。 这种方式会影响性能吗 有任何本机方法可以实现这一点吗?确实有一种本机方法:使用PL/SQL中的AQ JMS,请参阅。简而言之,您创建了一个具有JMS负载类型的AQ队列;然后您可以使用触发器中的PL/SQL发布消息。外部Java客户机可以连接到数据库并使用JMS读取消息 我不知道调用Java会对性能产生多大影

我有一个Oracle表的insert/update触发器。 是否有方法将受影响行(所有列)的详细信息作为消息发送给JMS? 我可以编写一个Java程序“loadjava”,并从触发器调用它。 这种方式会影响性能吗


有任何本机方法可以实现这一点吗?

确实有一种本机方法:使用PL/SQL中的AQ JMS,请参阅。简而言之,您创建了一个具有JMS负载类型的AQ队列;然后您可以使用触发器中的PL/SQL发布消息。外部Java客户机可以连接到数据库并使用JMS读取消息

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


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

加载Java有很多问题,而且不稳定如果加载了很多类和很多业务,请看一看

据我所知,甲骨文AQ不是免费的

在尝试了许多可能性之后,我实现了同样的需求,只创建了一个类,通过loadjava加载到oracle,该类被触发器称为过程,负责调用外部java程序和所有需要的参数,并将外部进程输出记录到表中,如下所示

我将文本mesage编码为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) {
            }
        }
    }
}
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) {
        }
    }
}