JDeveloper中開發JMS用戶端
在1.17.Weblogic建立JMS服務文章中,介紹了如何在weblogic中建立jms服務,下面要介紹如何通過java向jms隊列中寫入消息以及如何從jms隊列中取出消息。 要使用weblogic的jms,需要引入以下兩個包
-
javax.jms.jar
-
wlfullclient.jar
如果是使用jdeveloper開發,直接引入以下兩個Library即可
建立項目
工程建好如下
消息發送
實作類:JMSSender.java
java将消息發送到消息隊列中,需要經過以下步驟
- 連接配接jms伺服器
- 擷取連接配接工廠(Connection Factory)
- 通過連接配接工廠建立隊列連接配接(QueueConnection)
- 通過隊列連接配接建立隊列會話(QueueSession)
- 通過隊列會話建立隊列生産者(Sender/Product)
- 建立消息(Message)
- 通過生産者将消息發送到隊列中
具體代碼實作:
注意修改代碼中的相關參數:伺服器IP、weblogic使用者名、weblogic密碼、連接配接工廠名稱、隊列名稱
package cfesb.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伺服器
// 2. 擷取連接配接工廠(Connection Factory)
// 3. 通過連接配接工廠建立隊列連接配接(QueueConnection)
// 4. 通過隊列連接配接建立隊列會話(QueueSession)
// 5. 通過隊列會話建立隊列生産者(Sender/Product)
// 6. 建立消息(Message)
// 7. 通過生産者将消息發送到隊列中
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://10.18.113.84:8101");
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
properties.put(Context.SECURITY_CREDENTIALS, "weblogicPassword");
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");
}
}
Copy
運作程式後登入console,進入
domain->Services->Messaging->JMS Module->jms_test_module->jms_test_queue
在Monitoring頁面可以看到隊列中增加一條消息,點選
Show Messages
可以檢視消息詳細内容
消息接收
實作類:JMSReciver.java
java從消息隊列中擷取消息,需要經過以下步驟
- 連接配接jms伺服器
- 擷取連接配接工廠(Connection Factory)
- 通過連接配接工廠建立隊列連接配接(QueueConnection)
- 通過隊列連接配接建立隊列會話(QueueSession)
- 通過隊列會話建立隊列消費者(Reciver/Consumer)
- 接收消息(Message)
和消息發送到步驟差不多。 具體代碼實作:
注意修改代碼中的相關參數:伺服器IP、weblogic使用者名、weblogic密碼、連接配接工廠名稱、隊列名稱
package cfesb.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伺服器
// 2. 擷取連接配接工廠(Connection Factory)
// 3. 通過連接配接工廠建立隊列連接配接(QueueConnection)
// 4. 通過隊列連接配接建立隊列會話(QueueSession)
// 5. 通過隊列會話建立隊列消費者(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://10.18.113.84:8101");
properties.put(Context.SECURITY_PRINCIPAL, "weblogic");
properties.put(Context.SECURITY_CREDENTIALS, "weblogicPassword");
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();
}
}
Copy
用戶端調用可以看到控制台輸出了剛才寫入的消息:
查詢weblogic控制台中的消息隊列,可以看到消息已經被使用:
JMS用戶端
為了更清楚了解jms消息發送過程和更友善的測試,新增一個用戶端,該用戶端有兩種模式,當作為生産者可以在控制台輸入消息發送到消息隊列中,當作為消費者,一旦消息隊列中有消息産生,立刻将消息列印到控制台。 用戶端代碼如下:
package cfesb.demo.jms;
import java.util.Scanner;
import cfesb.demo.jms.JMSSender;
import cfesb.demo.jms.JMSReciver;
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隊列發送消息");
System.out.println("reciver:從隊列中取出消息");
}
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();
}
}
}
Copy
将代碼打包(關于如何打包參考1.20.JDeveloper部署jar可執行包),jar名稱為
jmsdemo.jar
,執行以下指令将用戶端作為生産者運作
java -jar jmsdemo.jar sender
Copy
重新打開一個控制台,執行以下指令将用戶端作為消費者運作
java -jar jmsdemo.jar reciver
Copy
在第一個控制台中輸入消息,消息立馬在第二個控制台輸出
目前為此我們已實作JMS消息的服務方設定+用戶端調用,但目前為止隻實作的消息的發送與接受,如果想實作更複雜的JMS功能,如在JMS收到消息時,向資料庫寫入日志,并傳回狀态等,這就需要使用EJB來實作JMS用戶端監聽,實時的接收到消息後進行功能處理,,我們将在下一章講解如何使用JMS實作日志記錄。
1.19.JDeveloper開發記錄日志功能的JMS