ActiveMQConnectionFactory();
ActiveMQConnectionFactory(String brokerURL);
ActiveMQConnectionFactory(String userName, String password, String b rokerURL) ;
ActiveMQConnectionFactory(String userName, String password, URI brok erURL) ;
ActiveMQConnectionFactory(URI brokerURL);
其中 brokerURL為ActiveMQ服務地址和端口。
例如:
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192. 168.0.135:61616");
或者
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
connectionFactory. setBrokerURL("tcp://192.168.0.135:61616");
</td>
</tr>
</tbody>
</table>
2、Connection
在成功創建正確的ConnectionFactory后,下一步將是創建一個連接,它是JMS定義的一個接口。ConnectionFactory負責返回可以與底層消息傳遞系統進行通信的Connection實現。通常客戶端只使用單一連接。根據JMS文檔,Connection的目的是“利用 JMS提供者封裝開放的連接”,以及表示“客戶端與提供者服務例程之間的開放TCP /IP套接字”。該文檔還指出 Connection應該是進行客戶端身份驗證的地方,除了其他一些事項外,客戶端還可以指定惟一標志符。
當一個Connection被創建時,它的傳輸默認是關閉的,必須使用start方法開啟。一個Connection可以建立一個或多個的Session。當一個程序執行完成后,必須關閉之前創建的Connection,否則 ActiveMQ不能釋放資源,關閉一個Connection同樣也關閉了 Session,MessageProducer和MessageConsumer。
Connection支持并發。
2.1、創建Connection
ActiveMQConnectionFactory方法:
Connection createConnection();
Connection createConnection(String userName, String password);
</td>
</tr>
</tbody>
</table>
2.2、開啟 Connection
void start();
如:connection.start();
</td>
</tr>
</tbody>
</table>
2.3關閉 Connection
void close();
如:connection.close();
</td>
</tr>
</tbody>
</table>
3、 Session
一旦從ConnectionFactory中獲得一個Connection,就必須從Connection中創建一個或者多個Session。Session是一個發送或接收消息的線程,可以使用Session創建 MessageProducer,MessageConsumer和Message。
Session可以被事務化,也可以不被事務化。通常可以通過向Connection上的適當創建方法傳遞一個布爾參數對此進行設置。
Session createSession(boolean transacted, int acknowledgeMode);
其中transacted為使用事務標識,acknowledgeMode為簽收模式。
如:Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
</td>
</tr>
</tbody>
</table>
4、 Destination
Destination是一個客戶端用來指定生產消息目標和消費消息來源的對象。
在PTP模式中,Destination被稱作Queue即隊列;在Pub/Sub模式,Destination被稱作Topic即主題。在程序中可以使用多個Queue和Topic。
Queue createQueue(String queueName);
TemporaryQueue createTemporaryQueue();
Topic createTopic(String topicName);
TemporaryTopic createTemporaryTopic();
如: Destination destination = session.createQueue("TEST.FOO");
</td>
</tr>
</tbody>
</table>
5、 MessageProducer
MessageProducer是一個由Session創建的對象,用來向Destination發送消息。
5.1、創建MessageProducer
MessageProducer createProducer(Destination destination);
如:MessageProducer producer = session.createProducer(destination);
</td>
</tr>
</tbody>
</table>
5.2 發送消息
void send(Destination destination, Message message);
void send(Destination destination, Message message, int deliveryMode, in tpriority, long timeToLive);
void send(Message message);
void send(Message message, int deliveryMode, int priority, long timeToLive);
其中deliveryMode為傳送模式,priority為消息優先級,timeToLive為消息過期時間。
如:producer.send(message);
</td>
</tr>
</tbody>
</table>
6、 MessageConsumer
MessageConsumer是一個由Session創建的對象,用來從Destination接收消息。
6.1、創建MessageConsumer
MessageConsumer createConsumer(Destination destination);
MessageConsumer createConsumer(Destination destination, String messageSelector);
MessageConsumer createConsumer(Destination destination, String messageSelector, boolean noLocal);
TopicSubscriber createDurableSubscriber(Topic topic, String name);
TopicSubscriber createDurableSubscriber(Topic topic, String name, String messageSelector, boolean noLocal);
其中messageSelector為消息選擇器;noLocal標志默認為false,當設置為true時限制消費者只能接收和自己相同的連接(Connection)所發布的消息,此標志只適用于主題,不適用于隊列;name標識訂閱主題所對應的訂閱名稱,持久訂閱時需要設置此參數。
如:MessageConsumer consumer = session.createConsumer(destination);
</td>
</tr>
</tbody>
</table>
6.2、消息的同步與異步接收
消息的同步接收是指客戶端主動去接收消息,客戶端可以采用MessageConsumer的receive方法去接收下一個消息。
消息的異步接收是指當消息到達時,ActiveMQ主動通知客戶端。客戶端可以通過注冊一個實現 MessageListener接口的對象到MessageConsumer。MessageListener只有一個必須實現的方法onMessage,它只接收一個參數,即Message。在為每個發送到Destination的消息實現onMessage時,將調用該方法。
| | | | | | | | |