java工作流和mq相結合可以采用以下3種方式:

創新互聯公司是一家專業從事成都網站設計、成都網站制作的網絡公司。作為專業網站建設公司,創新互聯公司依托的技術實力、以及多年的網站運營經驗,為您提供專業的成都網站建設、成都全網營銷及網站設計開發服務!
1、在工作流中使用消息隊列:可以使用消息隊列作為工作流中的任務處理引擎,將任務分配到消息隊列中,并通過消息隊列中的消息通知任務執行狀態或結果。
2、在消息隊列中使用工作流:可以使用工作流作為消息隊列中的消息處理引擎,將消息作為工作流中的任務,通過工作流引擎執行任務,并根據任務執行結果發送消息通知。
3、工作流和消息隊列相互協作:可以將工作流和消息隊列結合起來,實現復雜的任務處理和協作場景,例如多個任務之間的依賴關系、任務執行的順序控制、任務執行的并行處理等。
我用的方法是:
MQQueueManager?qMgr?=?new?MQQueueManager("BVMTEST");
System.out.println("queue?manager?is?connected!");
int?openOptions?=?MQC.MQOO_OUTPUT?|?MQC.MQOO_FAIL_IF_QUIESCING;
/*?打開隊列?*/
com.ibm.mq.MQQueue?queue?=?qMgr.accessQueue("test1",?openOptions);
然后在調用queue.getCurrentDepth()的方法的時候居然報了異常:
MQJE001:?完成代碼是?2,原因為?2038
如果我不在此處調用這個方法,而在后面進行
queue.put(outMsg,?new?MQPutMessageOptions());方法,居然可以成功放入測試信息.
給你一個有用的代碼大全:
密碼:exn4
import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import com.fxun.plant.vo.CommandVO;
public class ProducerTool extends Thread {
private Destination destination;
// private int messageCount = 500;
long sleepTime = 0;
// private boolean verbose = true;
// private int messageSize = 255;
private long timeToLive = 0; // 消息存活時間
private String user = ActiveMQConnection.DEFAULT_USER;
private String password = ActiveMQConnection.DEFAULT_PASSWORD;
private String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private String subject; // subject的名字,默認是TOOL.DEFAULT
// private boolean topic;
private boolean transacted = false; // 是否采用事務
// private boolean persistent = false;
private P2PQueue p2pQueue;
public ProducerTool(String user, String password, String url, String subject) {
this.user = user;
this.password = password;
this.url = url;
this.subject = subject;
}
public void run() {
Connection connection = null;
try {
// Create the connection.
ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(user, password, url);
connection = connectionFactory.createConnection();
connection.start();
// Create the session
Session session = connection.createSession(transacted, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
// Create the producer.
MessageProducer producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
producer.setTimeToLive(timeToLive);
CommandVO commandVO = null;
int size = 0;
while (true) {
size = p2pQueue.getSize();
if (size 0) {
BytesMessage message = session.createBytesMessage();
message.writeInt(size);
for (int i = 0; i size; i++) {
commandVO = p2pQueue.pool();
if(commandVO == null) {
message.writeInt(0);
} else {
message.writeInt(commandVO.getCountSize());
message.writeInt(commandVO.getCommand()); // 指令
message.writeBytes(commandVO.getContent()); // 內容
}
}
producer.send(message);
}
Thread.sleep(300);
}
// Use the ActiveMQConnection interface to dump the connection
// stats.
// ActiveMQConnection c = (ActiveMQConnection) connection;
// c.getConnectionStats().dump(new IndentPrinter());
} catch (Exception e) {
System.out.println("Caught: " + e);
e.printStackTrace();
} finally {
try {
connection.close();
} catch (Throwable ignore) {
}
}
}
public void setPassword(String pwd) {
this.password = pwd;
}
public void setSleepTime(long sleepTime) {
this.sleepTime = sleepTime;
}
public void setSubject(String subject) {
this.subject = subject;
}
public void setTimeToLive(long timeToLive) {
this.timeToLive = timeToLive;
}
public void setTransacted(boolean transacted) {
this.transacted = transacted;
}
public void setUrl(String url) {
this.url = url;
}
public void setUser(String user) {
this.user = user;
}
public P2PQueue getP2pQueue() {
return p2pQueue;
}
public void setP2pQueue(P2PQueue p2pQueue) {
this.p2pQueue = p2pQueue;
}
}
原代碼都發給你
當前標題:JAVA連接MQ代碼 java如何連接
當前URL:http://www.js-pz168.com/article26/ddjoscg.html
成都網站建設公司_創新互聯,為您提供面包屑導航、定制開發、關鍵詞優化、網頁設計公司、網站建設、微信公眾號
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯