JMS五種訊息的傳送 接收的例子

2021-06-05 22:57:50 字數 2268 閱讀 3444

1、訊息傳送

//連線工廠

connectionfactory connfactory = new activemqconnectionfactory(

activemqconnection.default_user,

activemqconnection.default_password,

"tcp://localhost:61616");

//連線到jms提供者

connection conn = connfactory.createconnection();

conn.start();

//事務性會話,自動確認訊息

session session = conn.createsession(true, session.auto_acknowledge);

//訊息的目的地

destination destination = session.createqueue("queue.hello");

//訊息生產者

messageproducer producer = session.createproducer(destination);

producer.setdeliverymode(deliverymode.non_persistent); //不持久化

//文字訊息

textmessage textmessage = session.createtextmessage("文字訊息");

producer.send(textmessage);

//鍵值對訊息

mapmessage mapmessage = session.createmapmessage();

mapmessage.setlong("age", new long(32));

mapmessage.setdouble("sarray", new double(5867.15));

mapmessage.setstring("username", "鍵值對訊息");

producer.send(mapmessage);

//流訊息

streammessage streammessage = session.createstreammessage();

streammessage.writestring("streammessage流訊息");

streammessage.writelong(55);

producer.send(streammessage);

//位元組訊息

string s = "bytesmessage位元組訊息";

bytesmessage bytesmessage = session.createbytesmessage();

bytesmessage.writebytes(s.getbytes());

producer.send(bytesmessage);

//物件訊息

user user = new user("cjm", "物件訊息"); //user物件必須實現serializable介面

objectmessage objectmessage = session.createobjectmessage();

objectmessage.setobject(user);

producer.send(objectmessage);

session.commit(); //在事務性會話中,只有commit之後,訊息才會真正到達目的地

producer.close();

session.close();

conn.close();

2、訊息接收:通過訊息***的方式接收訊息

public class receiver implements messagelistener

session.commit();

consumer.close();

session.close();

conn.close();

} public void onmessage(message m) else if(m instanceof mapmessage)else if(m instanceof streammessage)else if(m instanceof bytesmessage)

}else if(m instanceof objectmessage)else

stop = true;

}catch(jm***ception e)

}}

ActiveMQ五種訊息的傳送 接收

1.生產者 連線工廠 connectionfactory connfactory new activemqconnectionfactory activemqconnection.default user,activemqconnection.default password,tcp localho...

實驗五 訊息的傳送與接收

實驗五 訊息的傳送與接收 實驗目的 1 了解什麼是訊息 2 熟悉訊息傳送的機理 實驗內容 訊息的建立 傳送和接收。使用系統呼叫msgget msgsnd msgrev 及msgctl 編制一長度為 k的訊息傳送和接收的程式。void client exit 0 main 2 server.c inc...

實驗五 訊息的傳送與接收

實驗五 訊息的傳送與接收 實驗目的 1 了解什麼是訊息 2 熟悉訊息傳送的機理 實驗內容 訊息的建立 傳送和接收。使用系統呼叫msgget msgsnd msgrev 及msgctl 編制一長度為 k的訊息傳送和接收的程式。void client exit 0 main 2 server.c inc...