5.2. 建立连接
本例创建一个新连接池,将它绑定到连接工厂,并使用 池创建新连接。
示例:建立连接 - Connect.java
package net.example; import javax.jms.Connection; import javax.jms.ConnectionFactory; import org.apache.qpid.jms.JmsConnectionFactory; import org.messaginghub.pooled.jms.JmsPoolConnectionFactory; public class Connect { public static void main(String[] args) throws Exception { if (args.length != 1) { System.err.println("Usage: Connect <connection-uri>"); System.exit(1); } String connUri = args[0]; ConnectionFactory factory = new JmsConnectionFactory(connUri); JmsPoolConnectionFactory pool = new JmsPoolConnectionFactory(); try { pool.setConnectionFactory(factory); Connection conn = pool.createConnection(); conn.start(); try { System.out.println("CONNECT: Connected to '" + connUri + "'"); } finally { conn.close(); } } finally { pool.stop(); } } }