ActiveMQ生产者从PostgreSQL表中的内容发送消息

时间:2017-03-27 10:03:53

标签: java sql postgresql activemq

是否有人为我提供了一个ActiveMQ生产者编码从PostgreSQL中的表内的内容发送消息的参考,但我希望我可以发送表上的所有行成为一个一个消息... 我得到了一个人的参考资料,这个参考资料的说法与我上面的相同,但它告诉我如何从.txt文件中的内容中获取所有消息... here the referance

到目前为止我得到了这个:

package testMQDB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class ProducerDB {

   public static void ProducerDB(String[] args){
       Connection c = null;
       Statement stmt = null;
       try {
       Class.forName("org.postgresql.Driver");
         c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"TestDB, "admin", "admin");
         c.setAutoCommit(false);
         System.out.println("----------------------------");

         stmt = c.createStatement();
         ResultSet rs = stmt.executeQuery( "SELECT * FROM MESSAGES;" );
         while ( rs.next() ) {
            String  message = rs.getString("MESSAGE");
            System.out.println( "Message = " + message );
            if (message.equals(true)){
                // write the ActiveMQ code here? 
            }
         }
         rs.close();
         stmt.close();
         c.close();
       } catch ( Exception e ) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
       }
       System.out.println("----------------------------");
       System.out.println("Message sent successfully");
   }
}

2 个答案:

答案 0 :(得分:2)

为什么不使用骆驼来简化?

https://book.cakephp.org/3.0/en/orm/saving-data.html#merging-request-data-into-entities

来自(“sql:select * from table?maxMessagesPerPoll = 1& dataSource = pg”).to(“activemq:queue:customers”);

<bean id="pg" 
class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="org.postgresql.Driver"/> 
        <property name="url" value="jdbc:postgresql://localhost:5432/ddd"/> 
        <property name="username" value="user"/> 
        <property name="password" value="password"/> 
    </bean> 

<route> 
      <description>select 1 row</description> 
      <from uri="sql:select * from table?maxMessagesPerPoll=1&dataSource=pg" /> 
      <to  uri="activemq:queue:customers"/>
</route> 

http://camel.apache.org/sql-component.html https://github.com/apache/camel/tree/master/examples/camel-example-sql

http://camel.apache.org/jms.html

http://activemq.apache.org/sample-camel-routes.html

<强>更新

您可以优化代码:

public static void ProducerDB(String[] args){
    ConnectionFactory factory = null;
    javax.jms.Connection connection = null;
    Session session = null;
    Destination destination = null;
    MessageProducer producer = null;
    Connection c = null;
    Statement stmt = null;
    try {
    Class.forName("org.postgresql.Driver");
    c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"TestDB, "admin", "admin");
    c.setAutoCommit(false);
    System.out.println("----------------------------");
    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);

    stmt = c.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM MESSAGES;");
    while (rs.next()) {
        String  message = rs.getString("MESSAGE");
        System.out.println("Message = " + message);
            try {
                TextMessage mssg = session.createTextMessage(message);
                System.out.println("Sent: " + mssg.getText());
                producer.send(mssg);
            }
            catch (JMSException e) {
                e.printStackTrace();
            }
    }
    }catch (Exception e) {
        System.err.println(e.getClass().getName()+": "+ e.getMessage());
    }finally{
        if (c != null) {
            c.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
    System.out.println("----------------------------");
    System.out.println("Message sent successfully");
}

答案 1 :(得分:0)

得到答案......

package TestMQDB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
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 ProducerDB {

    public static void ProducerDB(String[] args){
        ConnectionFactory factory = null;
        javax.jms.Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        Connection c = null;
        Statement stmt = null;
        try {
        Class.forName("org.postgresql.Driver");
        c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/"TestDB, "admin", "admin");
        c.setAutoCommit(false);
        System.out.println("----------------------------");

        stmt = c.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM MESSAGES;");
        while (rs.next()) {
            String  message = rs.getString("MESSAGE");
            System.out.println("Message = " + message);
                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);
                    TextMessage mssg = session.createTextMessage(message);
                    System.out.println("Sent: " + mssg.getText());
                    producer.send(mssg);
                }
                catch (JMSException e) {
                    e.printStackTrace();
                }

        }
        rs.close();
        stmt.close();
        c.close();
        }
        catch (Exception e) {
            System.err.println(e.getClass().getName()+": "+ e.getMessage());
        }
        System.out.println("----------------------------");
        System.out.println("Message sent successfully");
    }
}