淺談-訊息服務

運行平台:

開發工具:

JMS 介紹:

1. 佇列〈Queue〉

2. 主題〈Topic〉

原始程式:

訊息處理程式:

CDRQ.java

Message Driven Bean

定義訊息佇列:

CDRQ-service.xml

佈署於服務器內

訊息發送程式:

Client.java

J2EE 客戶端程式

執行批次檔:

Run.bat

訊息發送程式

日誌定義檔:

log4j.properties

訊息發送程式

企業服務匯流排:

Enterprise Service Bus

MDB

CDRQ.rar

package com.emprogria.queue; import java.util.logging.Level; import java.util.logging.Logger; import javax.ejb.ActivationConfigProperty; import javax.ejb.MessageDriven; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.ObjectMessage; @MessageDriven(mappedName = "CDRQ", activationConfig = { @ActivationConfigProperty(propertyName = "messagingType", propertyValue = "javax.jms.MessageListener"), @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"), @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue"), @ActivationConfigProperty(propertyName = "destination", propertyValue = "/queue/CDRQ") }) public class CDRQ implements MessageListener { private Logger myLogger = null; public CDRQ() { this.myLogger = Logger.getLogger(this.getClass().getName()); } public void onMessage(Message message) { boolean OK = false; try { if (message instanceof ObjectMessage) { ObjectMessage myObjectMessage = (ObjectMessage) message; this.myLogger.info(myObjectMessage.getObject().toString()); OK = true; } else { this.myLogger.log(Level.WARNING, "消息物件型態錯誤:" + message.getJMSMessageID()); } if (OK) { } else { this.myLogger.info(message.getJMSMessageID()); } } catch (JMSException ex) { this.myLogger.log(Level.SEVERE, ex.getMessage()); } } }

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <server> <mbean code="org.jboss.jms.server.destination.QueueService" name="jboss.messaging.destination:service=Queue,name=CDRQ" xmbean-dd="xmdesc/Queue-xmbean.xml"> <attribute name="JNDIName">/queue/CDRQ</attribute> <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends> <depends>jboss.messaging:service=PostOffice</depends> </mbean> </server>

package com.emprogria.cdrq; import java.io.File; import java.io.Serializable; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; import javax.jms.JMSException; import javax.jms.ObjectMessage; import javax.jms.Queue; import javax.jms.QueueConnection; import javax.jms.QueueConnectionFactory; import javax.jms.QueueSender; import javax.jms.QueueSession; import javax.naming.InitialContext; public class Client { private QueueConnection myQueueConnection = null; private QueueSender myQueueSender = null; private QueueSession myQueueSession = null; private String log4jConfigurationFile = "log4j.properties"; public Client() { File myFile = new File(log4jConfigurationFile); org.apache.log4j.PropertyConfigurator.configure(myFile.getAbsolutePath()); } public Client(String log4jConfigurationFile) { File myFile = new File(log4jConfigurationFile); org.apache.log4j.PropertyConfigurator.configure(myFile.getAbsolutePath()); this.log4jConfigurationFile = log4jConfigurationFile; } public boolean OpenJMS(String hostName, int tcpPort) { boolean OK = false; Properties myProperties = new Properties(); InitialContext myInitialContext = null; QueueConnectionFactory myQueueConnectionFactory = null; Queue myQueue = null; myProperties.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory"); myProperties.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces"); myProperties.setProperty("java.naming.provider.url", String.format("jnp://%s:%d", new Object[]{hostName, new Integer(tcpPort)})); try { myInitialContext = new InitialContext(myProperties); myQueue = (Queue) myInitialContext.lookup("queue/CDRQ"); myQueueConnectionFactory = (QueueConnectionFactory) myInitialContext.lookup("ConnectionFactory"); this.myQueueConnection = myQueueConnectionFactory.createQueueConnection(); this.myQueueSession = this.myQueueConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); this.myQueueSender = myQueueSession.createSender(myQueue); OK = true; } catch (Exception ex) { try { if (this.myQueueConnection != null) { this.myQueueConnection.close(); } if (this.myQueueSession != null) { this.myQueueSession.close(); } if (this.myQueueSender != null) { this.myQueueSender.close(); } } catch (JMSException e) { } Logger.getLogger(this.getClass().getName() + ".OpenJMS").log(Level.SEVERE, ex.getMessage()); } return OK; } public boolean CloseJMS() { boolean OK = false; try { if (this.myQueueSender != null) { this.myQueueSender.close(); } if (this.myQueueConnection != null) { this.myQueueConnection.close(); } OK = true; } catch (Exception ex) { Logger.getLogger(this.getClass().getName() + ".CloseJMS").log(Level.SEVERE, ex.getMessage()); } return OK; } private boolean Send(Serializable message) { boolean OK = false; try { if (this.myQueueSession != null && this.myQueueSender != null) { ObjectMessage myObjectMessage = this.myQueueSession.createObjectMessage(); myObjectMessage.setObject(message); this.myQueueSender.send(myObjectMessage); OK = true; } } catch (Exception ex) { Logger.getLogger(this.getClass().getName() + ".Send").log(Level.SEVERE, ex.getMessage()); } return OK; } /** * @param args 命令列參數 */ public static void main(String[] args) { String hostName = "localhost"; int tcpPort = 2099; Client myClient = new Client(); if (myClient.OpenJMS(hostName, tcpPort)) { String message; for (int i = 0; i < 10; i++) { if (args.length > 0) { message = String.valueOf(i) + ":" + args[0]; } else { message = String.valueOf(i) + ":" + myClient.toString(); } if (myClient.Send(message)) { Logger.getLogger(myClient.getClass().getName() + ".Main").info(message); } } myClient.CloseJMS(); } } }

SET JBOSS_HOME=D:\jBoss6.M2 SET CLASSPATH=. java -cp %JBOSS_HOME%\lib\*;%JBOSS_HOME%\client\*;%JBOSS_HOME%\server\default\lib\*;..\dist\CDRQ-app-client.jar;. ^ com.emprogria.cdrq.Client AABBCC

log4j.rootLogger=INFO, A log4j.appender.A=org.apache.log4j.ConsoleAppender log4j.appender.A.layout=org.apache.log4j.PatternLayout log4j.appender.A.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n