在上一篇文章中,介紹了如何在weblogic中創(chuàng)建jms相關(guān)資源显熏,下面要介紹如何通過(guò)java向jms隊(duì)列中寫(xiě)入消息以及如何從jms隊(duì)列中取出消息蔚万。
要使用weblogic的jms蚁飒,需要引入以下兩個(gè)包
javax.jms.jar
-
wlfullclient.jar
如果是使用jdeveloper開(kāi)發(fā)袖瞻,直接引入以下兩個(gè)Library即可
消息發(fā)送
java將消息發(fā)送到消息隊(duì)列中迷扇,需要經(jīng)過(guò)以下步驟
- 連接jms服務(wù)器
- 獲取連接工廠(chǎng)(Connection Factory)
- 通過(guò)連接工廠(chǎng)創(chuàng)建隊(duì)列連接(QueueConnection)
- 通過(guò)隊(duì)列連接創(chuàng)建隊(duì)列會(huì)話(huà)(QueueSession)
- 通過(guò)隊(duì)列會(huì)話(huà)創(chuàng)建隊(duì)列生產(chǎn)者(Sender/Product)
- 創(chuàng)建消息(Message)
- 通過(guò)生產(chǎn)者將消息發(fā)送到隊(duì)列中
具體代碼實(shí)現(xiàn):
package asan.demo.jms;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JMSSender {
private QueueSender sender = null;
private QueueSession session = null;
private static final String JMS_FACTORY_JNDI = "jms/jms_test_connection_factory1";
private static final String JMS_QUEUE_JNDI = "jms/jms_test_queue";
public JMSSender() {
super();
}
public void sendMessage(String msg) {
TextMessage textMsg;
try {
if (this.sender == null) {
this.init();
}
textMsg = session.createTextMessage();
textMsg.setText(msg);
sender.send(textMsg);
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 1. 連接jms服務(wù)器
// 2. 獲取連接工廠(chǎng)(Connection Factory)
// 3. 通過(guò)連接工廠(chǎng)創(chuàng)建隊(duì)列連接(QueueConnection)
// 4. 通過(guò)隊(duì)列連接創(chuàng)建隊(duì)列會(huì)話(huà)(QueueSession)
// 5. 通過(guò)隊(duì)列會(huì)話(huà)創(chuàng)建隊(duì)列生產(chǎn)者(Sender/Product)
// 6. 創(chuàng)建消息(Message)
// 7. 通過(guò)生產(chǎn)者將消息發(fā)送到隊(duì)列中
private void init() throws NamingException, JMSException {
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
properties.put(Context.SECURITY_CREDENTIALS, "weblogic1");
InitialContext ctx = new InitialContext(properties);
QueueConnectionFactory jmsFactory =
(QueueConnectionFactory)ctx.lookup(JMS_FACTORY_JNDI);
QueueConnection jmsConn = jmsFactory.createQueueConnection();
session = jmsConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue)ctx.lookup(JMS_QUEUE_JNDI);
sender = session.createSender(queue);
}
public static void main(String[]cmd){
JMSSender sender=new JMSSender();
sender.sendMessage("hello world");
}
}
運(yùn)行程序后登錄console瑟蜈,進(jìn)入domain->Services->Messaging->JMS Module->jms_test_module->jms_test_queue
在Monitoring頁(yè)面可以看到隊(duì)列中增加一條消息,點(diǎn)擊Show Messages
可以查看消息詳細(xì)內(nèi)容
消息接收
java從消息隊(duì)列中獲取消息姐帚,需要經(jīng)過(guò)以下步驟
- 連接jms服務(wù)器
- 獲取連接工廠(chǎng)(Connection Factory)
- 通過(guò)連接工廠(chǎng)創(chuàng)建隊(duì)列連接(QueueConnection)
- 通過(guò)隊(duì)列連接創(chuàng)建隊(duì)列會(huì)話(huà)(QueueSession)
- 通過(guò)隊(duì)列會(huì)話(huà)創(chuàng)建隊(duì)列消費(fèi)者(Reciver/Consumer)
- 接收消息(Message)
和消息發(fā)送到步驟差不多吏垮。
具體代碼實(shí)現(xiàn):
package asan.demo.jms;
import java.util.Hashtable;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class JMSReciver {
private MessageConsumer reciver = null;
private static final String JMS_FACTORY_JNDI = "jms/jms_test_connection_factory1";
private static final String JMS_QUEUE_JNDI = "jms/jms_test_queue";
public JMSReciver() {
super();
}
public void reciveMessage() {
try {
if (this.reciver == null) {
this.init();
}
System.out.println("waiting to recive message from jms queue "+JMS_QUEUE_JNDI);
while(true){
Message msg=reciver.receive();
if(msg instanceof TextMessage){
TextMessage textMsg=(TextMessage)msg;
System.out.println("recive jms message:"+textMsg.getText());
}
}
} catch (JMSException e) {
e.printStackTrace();
} catch (Exception ex) {
ex.printStackTrace();
}
}
// 1. 連接jms服務(wù)器
// 2. 獲取連接工廠(chǎng)(Connection Factory)
// 3. 通過(guò)連接工廠(chǎng)創(chuàng)建隊(duì)列連接(QueueConnection)
// 4. 通過(guò)隊(duì)列連接創(chuàng)建隊(duì)列會(huì)話(huà)(QueueSession)
// 5. 通過(guò)隊(duì)列會(huì)話(huà)創(chuàng)建隊(duì)列消費(fèi)者(Reciver/Consumer)
// 6. 接收消息(Message)
private void init() throws NamingException, JMSException {
Hashtable properties = new Hashtable();
properties.put(Context.INITIAL_CONTEXT_FACTORY,
"weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
properties.put(Context.SECURITY_CREDENTIALS, "weblogic1");
InitialContext ctx = new InitialContext(properties);
QueueConnectionFactory jmsFactory =
(QueueConnectionFactory)ctx.lookup(JMS_FACTORY_JNDI);
QueueConnection jmsConn = jmsFactory.createQueueConnection();
QueueSession session = jmsConn.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
Queue queue = (Queue)ctx.lookup(JMS_QUEUE_JNDI);
reciver = session.createReceiver(queue);
jmsConn.start();
}
public static void main(String[]cmd){
JMSReciver consumer=new JMSReciver();
consumer.reciveMessage();
}
}
運(yùn)行程序,控制臺(tái)打印出隊(duì)列中消息
JMS客戶(hù)端
為了更清楚了解jms消息發(fā)送過(guò)程,這邊寫(xiě)了一個(gè)客戶(hù)端罐旗,該客戶(hù)端有兩種模式膳汪,當(dāng)作為生產(chǎn)者可以在控制臺(tái)輸入消息發(fā)送到消息隊(duì)列中,當(dāng)作為消費(fèi)者九秀,一旦消息隊(duì)列中有消息產(chǎn)生遗嗽,立刻將消息打印到控制臺(tái)。
客戶(hù)端代碼如下:
package asan.demo.jms;
import java.util.Scanner;
public class JMSClient {
public JMSClient() {
super();
}
public static void help() {
System.out.println("Usage:java -jar JMSClient.jar sender/reciver");
System.out.println("sender:向jms隊(duì)列發(fā)送消息");
System.out.println("reciver:從隊(duì)列中取出消息");
}
public static void main(String[] cmd) {
if (cmd.length == 0) {
help();
return;
}
String mode = cmd[0];
if ("sender".equalsIgnoreCase(mode)) {
JMSSender sender = new JMSSender();
Scanner sc = new Scanner(System.in);
System.out.println("input you message(input end to exit):");
while (true) {
String msg = sc.nextLine();
if ("end".equalsIgnoreCase(msg)) {
return;
}
sender.sendMessage(msg);
}
} else {
JMSReciver consumer = new JMSReciver();
consumer.reciveMessage();
}
}
}
將代碼打包(關(guān)于如何打包參考這篇文章)鼓蜒,jar名稱(chēng)為JMSClient.jar
痹换,執(zhí)行以下命令將客戶(hù)端作為生產(chǎn)者運(yùn)行
java -jar JMSClient.jar sender
重新打開(kāi)一個(gè)控制臺(tái),執(zhí)行以下命令將客戶(hù)端作為消費(fèi)者運(yùn)行
java -jar JMSClient.jar reciver
在第一個(gè)控制臺(tái)中輸入消息友酱,消息立馬在第二個(gè)控制臺(tái)輸出
程序稍加改造就能變成一個(gè)實(shí)時(shí)點(diǎn)對(duì)點(diǎn)聊天程序晴音,思路是在weblogic中創(chuàng)建兩個(gè)隊(duì)列每個(gè)客戶(hù)端對(duì)應(yīng)一個(gè)隊(duì)列柔纵,兩個(gè)客戶(hù)端分別向?qū)Ψ疥?duì)列發(fā)送消息并從自己的隊(duì)列中獲取消息缔杉。