(转)JMS事务

转:http://blog.csdn.net/jixiuffff/article/details/5780834

事务
         session = conn.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
         true 表示这个会话是事务性的,需要显式的commit 或者rollback ()
                                         没有begin()方法,眨动打开事务
                                        
     public void send() {
        try {
                    publicMessageFlag("start");
                   publicMessage("msg1");
                    publicMessage("msg2");
                    publicMessage("msg3");
                    publicMessageFlag("end");
                    session.commit();
         } catch (JMSException e) {
                 e.printStackTrace();
            try {
                 session.rollback();
            } catch (JMSException e1) {
                e1.printStackTrace();
            }
        }//发送一个空的bytemessage 消息,标志着这一组消息的开始
    }

package org.jixiuf.chat;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.jixiuf.util.JNDIUtil;
public class ChatServer {
    QueueConnection conn;
    Queue requestQueue;
    QueueSession session;
    int age = 10;
    QueueSender sender;
    public ChatServer() {
        conn = JNDIUtil.crateQueueConnection();
        requestQueue = JNDIUtil.lookupQueue("requestQueue");
        try {
            session = conn.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
            sender = session.createSender(requestQueue);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
    // flag 是"start" ,和"end" 分别表示这组消息的开头
    public void publicMessageFlag(String flag) throws JMSException {
        BytesMessage msg = null;
     
            msg = session.createBytesMessage();
            msg.setStringProperty("JMSXGroupID", "group1");
            msg.setStringProperty("flag", flag);
            sender.send(msg);
    }
    // flag 是"start" ,和"end" 分别表示这组消息的开头
    public void publicMessage(String msgStr)throws JMSException {
        TextMessage msg;
     
            msg = session.createTextMessage();
            msg.setStringProperty("JMSXGroupID", "group1");
            msg.setText(msgStr);
            sender.send(msg);
     
    }
    public void send() {
        try {
            publicMessageFlag("start");
            publicMessage("msg1");
            publicMessage("msg2");
            publicMessage("msg3");
            publicMessageFlag("end");
            session.commit();
        } catch (JMSException e) {
            e.printStackTrace();
            try {
                session.rollback();
            } catch (JMSException e1) {
                e1.printStackTrace();
            }
        }//发送一个空的bytemessage 消息,标志着这一组消息的开始
        
    }
    public void exit() {
        try {
            conn.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
        System.exit(0);
    }
    public static void main(String[] args) {
        ChatServer server = new ChatServer();
        server.send();
        server.exit();
    }
}

 

posted @ 2013-07-20 03:54  wangle100  阅读(366)  评论(0编辑  收藏  举报