轉自:http://blog.synyx.de/2011/10/sending-jms-from-oracledb-to-external-activemq-broker/。
Sending JMS from OracleDB to external ActiveMQ Broker
October 18, 2011 | Posted in Developer Blog
Prerequisites
- Oracle 11gR2 (providing JDK 1.5)
- Active MQ 5.4.2 (the last version that can be build using JDK 1.5)
- Spring 3.0.5
Preparation of user privileges
call dbms_java.grant_permission( 'TEST', 'SYS:java.lang.RuntimePermission', 'getClassLoader', '' );
call dbms_java.grant_permission( 'TEST', 'SYS:java.util.PropertyPermission', '*', 'read,write' );
call dbms_java.grant_permission( 'TEST', 'SYS:java.net.SocketPermission', '*', 'listen,resolve');
call dbms_java.grant_permission( 'TEST', 'SYS:java.net.SocketPermission', '*', 'accept,resolve');
call dbms_java.grant_permission( 'TEST', 'SYS:java.net.SocketPermission', '*', 'connect,resolve');
A small prototype
<!-- create message broker - instead of using a somewhere centralized activemq server --> <bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean"> <property name="config" value="classpath:META-INF/activemq.xml" /> <property name="start" value="true" /> </bean> <!-- a simple test queue - the queue name is specified by the bean id --> <bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue" /> <!-- JMS connection factory (wrapped into a pooling connection factory) --> <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616" /> </bean> </property> </bean> <!-- simple message listener just logging received message to stdout --> <bean id="simpleListener" class="com.synyx.prototype.jms.SimpleMessageListener" /> <!-- listener container delegating to listener instances and wiring them to their destinations --> <jms:listener-container concurrency="10" connection-factory="jmsFactory"> <jms:listener id="queueListener" destination="testQueue" ref="simpleListener" /> </jms:listener-container>
public class ActiveMQQueueConnectionFactory implements QueueConnectionFactory {
private ConnectionFactory connectionFactory = null;
public ActiveMQQueueConnectionFactory(String brokerUrl) {
this.connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
}
public QueueConnection createQueueConnection() throws JMSException {
return (QueueConnection) createConnection();
}
public QueueConnection createQueueConnection(String username, String password) throws JMSException {
return createQueueConnection();
}
public Connection createConnection() throws JMSException {
return this.connectionFactory.createConnection();
}
public Connection createConnection(String username, String password) throws JMSException {
return createConnection();
}
}
public class QueueMessageSender {
private QueueConnectionFactory connectionFactory = null;
public QueueMessageSender(QueueConnectionFactory connectionFactory) {
this.connectionFactory = connectionFactory;
}
public void sendMessage(String destination, String message) throws JMSException {
QueueConnection connection = null;
try {
connection = this.connectionFactory.createQueueConnection();
QueueSession session = connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = session.createQueue(destination);
QueueSender sender = session.createSender(queue);
TextMessage textMessage = session.createTextMessage(message);
sender.send(textMessage);
} finally {
if (null != connection) {
connection.close();
}
}
}
}
public class JMSFromOracleTest {
private static final String BROKER_URL = "tcp://192.168.x.x:61616";
private static final String QUEUE_NAME = "testQueue";
public static void sendMessage(String message) throws JMSException {
QueueConnectionFactory connectionFactory = new ActiveMQQueueConnectionFactory(BROKER_URL);
QueueMessageSender sender = new QueueMessageSender(connectionFactory);
sender.sendMessage(QUEUE_NAME, message);
}
}
Packaging and Deployment to the Oracle DB
loadjava -v -r -u test/12345 -resolver "((* TEST) (* PUBLIC) (* -))" JMSSender-1.0-SNAPSHOT-jar-with-dependencies.jar
CREATE OR REPLACE PROCEDURE sendJmsMessage(message IN VARCHAR2)
AS LANGUAGE JAVA NAME 'com.synyx.prototype.jms.JMSFromOracleTest.sendMessage(java.lang.String)';
call sendjmsmessage('hello from oracle');