jms在jboss上的简单应用
核心概念:
?連接工廠(ConnectionFactory)客戶端用來創建連接的管理對象。 ?連接(Connection)代表一個與JMS提供者的活動連接。 ?目的(Destination)標識消息接收方式。 ?會話(Session)接收和發送消息的會話線程。 ?消息生產者(MessageProducer)會話使用它把消息發送到目的地。 ?消息消費者(MessageConsumer)會話使用它從目的地接收消息生產者發送的消息 1.發送消息的客戶端使用JMS的過程(1)使用JNDI查詢管理對象ConnectionFactory和Destination
(2)使用管理對象ConnectionFactory建立連接Connection
(3)使用連接Connection建立會話Session
(4)使用會話Session和管理對象Destination創建消息生產者MessagerProducer
(5)使用消息生產者MessagerPriducer發送消息
?
2.接收消息的客戶端使用JMS的過程
(1)使用JNDI查詢管理對象ConnectionFactory和Destination
(2)使用管理對象ConnectionFactory建立連接Connection
(3)使用連接Connection建立會話Session
(4)使用會話Session和管理對象Destination創建消息生產者MessagerProducer
(5)使用消息生產者MessagerConsumer接收消息
3. JMS中支持兩種事務方式:事務性會話和JTA事務
a)創建事務性會話的代碼:qsession=qcon.createQueueSession(true; //在PTP方式下創建事務性會話
Session.AUTO_ACKNOWLEDGE
);
tsession=tcon.createTopicSession(
true; //在Pub/Sub方式下創建事務性會話
Session.AUTO_ACKNOWLEDGE
);
b)JTA事務:JTA支持跨數據源的事務,步驟如下:
(1)創建非事務性會話
QueuesSession session=connection.createQueueSession(
???????????????????????? false,Session.AUTO_ACKNOWLEGE);
(2)使用JNDI查詢JTA事務引用
Context ctx = new InitialContext();
UserTansaction ux=(UserTansaction)ctx.lookup
(“javax.transaction.UserTansaction”);
(3)開始事務
ux.begin();
(4)執行業務操作
(5)提交或回滾事務
提交事務:ux.commit();
回滾事務:ux.rollback();
服務區端代碼
package test.jms;
import javax.jms.*;
import javax.naming.*;
public class Server {
? private static Server instance = new Server();
? private? TopicSession tsession = null;
? private? Topic topic = null;
? private? TopicPublisher tpub = null;
? private TopicConnectionFactory tcf = null;
? private TopicConnection tconn = null;
? private Context ctx = null;
? private Server() {
??? init();
? }
? public static Server getInstance(){
??? return instance;
? }
? public void sendMessage(MessageInfo msgInfo){
??? int tryTimes = 0;
????? while(true){
??????? try {
????????? if(msgInfo == null){
??????????? break;
????????? }
????????? Message msg = tsession.createMessage();
????????? msg.setStringProperty("xxxx", msgInfo.getxxxx());
????????? msg.setStringProperty("xxxx", msgInfo.getxxxx());
????????? msg.setStringProperty("xxxx", msgInfo.getxxxx());
????????? msg.setStringProperty("xxxx", msgInfo.getxxxx());
????????? msg.setStringProperty("xxxx", msgInfo.getxxxx());
????????? msg.setIntProperty("xxxx", msgInfo.getxxxx());
????????? tpub.publish(msg);
????????? break;
??????? }
??????? catch (Exception e) {
????????? if (!init()) {
??????????? if (tryTimes < 20) { //默認15分鐘可以恢復數據庫連接,這里冗余一部分
????????????? tryTimes++;
????????????? try{
??????????????? Thread.sleep(60000); //一分種后重試
????????????? }
????????????? catch(Exception ex){
??????????????? Logger.log(Logger.DEBUG_TYPE,ex);
????????????? }
????????????? continue;
??????????? }
??????????? else{
????????????? System.out.println("系統消息機制異常,系統將自動退出!請進行系統恢復!");
????????????? SysTool.exit(0);
????????????? return;
??????????? }
????????? }
??????? }
????? }
? }
? private boolean init() {
??? try {
????? try{
??????? tpub.close();
????? }
????? catch(Exception ex){ }
????? try{
??????? tsession.close();
????? }
????? catch(Exception ex){? }
????? try{
??????? tconn.close();
????? }
????? catch(Exception ex){? }
????? try{
??????? ctx.close();
????? }
????? catch(Exception ex){ }
????? ctx = new InitialContext();
????? tcf = (TopicConnectionFactory)ctx.lookup(
??????? "ConnectionFactory");
????? tconn = tcf.createTopicConnection();
????? tsession = tconn.createTopicSession(false,TopicSession.AUTO_ACKNOWLEDGE);
????? topic = (Topic)ctx.lookup("topic/xxxxTopic");
????? tpub = tsession.createPublisher(topic);
????? tconn.start();
????? return true;
??? }
??? catch (Exception e) {
????? Logger.log(Logger.DEBUG_TYPE, e);
????? return false;
??? }
? }
}
客戶端代碼
package test;
import java.util.Hashtable;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;
import javax.jms.TopicConnection;
import javax.jms.TopicConnectionFactory;
import javax.jms.TopicSession;
import javax.jms.TopicSubscriber;
import javax.naming.Context;
import javax.naming.InitialContext;
public class JMSClinet {
? public static void main(String[] args) {
??????? while(true){
??????? try {
??????????? TopicSession tsession = null;
??????????? TopicSubscriber tsub = null;
??????????? Hashtable ht = new Hashtable();
??????????? ht.put(Context.INITIAL_CONTEXT_FACTORY,
??????????????????? "org.jnp.interfaces.NamingContextFactory");
??????????? ht.put(Context.PROVIDER_URL, "ip地址:1099");
??????????? ht.put("java.naming.rmi.security.manager", "yes");
??????????? ht.put(Context.URL_PKG_PREFIXES, "org.jboss.naming");
??????????? Context ctx = new InitialContext(ht);
??????????? TopicConnectionFactory factory = (TopicConnectionFactory) ctx
??????????????????? .lookup("ConnectionFactory");
??????????? TopicConnection connection = factory.createTopicConnection();
??????????? TopicSession session = connection.createTopicSession(false,
??????????????????? Session.AUTO_ACKNOWLEDGE);
??????????? Topic topic = (Topic) ctx.lookup("topic/logInAndOutTopic");
??????????? tsub = session.createSubscriber(topic);
??????????? connection.start();
??????????? Message msg = tsub.receive();
??????????? String xxxx= msg.getStringProperty("xxxx");
??????????? String xxxx= msg.getStringProperty("xxxx");
??????????? String xxxx= msg.getStringProperty("xxxx");
??????????? String xxxx= msg.getStringProperty("xxxx");
??????????? String xxxx= msg.getStringProperty("xxxx");
??????????? int xxxx= msg.getIntProperty("xxxx");?????
??????????? connection.close();
??????? }
??????? catch (Exception e) {
??????????? e.printStackTrace();
??????? }
??????? }
??? }
}
配置文件
?<mbean code="org.jboss.mq.server.jmx.Topic"
? name="jboss.mq.destination:service=Topic,name=xxxxTopic">
??? <depends optional-attribute-name="DestinationManager">jboss.mq:service=DestinationManager</depends>
??? <depends optional-attribute-name="SecurityManager">jboss.mq:service=SecurityManager</depends>
??? <attribute name="SecurityConf">
?????? <security>
???????? <role name="xxxx" read="true" write="true"/>
???????? <role name="xxxx" read="true" write="true" create="false"/>
???????? <role name="xxxx" read="true" write="true" create="true"/>
????? </security>
??? </attribute>
?</mbean>
轉載于:https://www.cnblogs.com/davidwang456/archive/2013/01/22/2871924.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的jms在jboss上的简单应用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: spring事务模板使用
- 下一篇: spring调用存储过程