0
나는 온통 인터넷을 찾고 있습니다. 나는 cleansession "false"와 qos 1과 2를 설정하려고 시도했지만 아직 온라인에 올 때 구독자는 모든 콘텐츠를 얻지 못하고 있습니다. 도와주세요 ... 내 코드구독자가 Mqtt 주제에서 비활성 상태 일 때 제작자가 보낸 모든 데이터를 얻는 방법 Java (보유하지 않거나 마지막 메시지가 아님)
Example.java (프로듀서)
public class Example extends PersonBean {
public void hey(){
String clientId = MqttClient.generateClientId();
MemoryPersistence persistence = new MemoryPersistence();
PersonBean pb=new PersonBean();
for(int i=1;i<=5;i++){
Gson gson = new Gson();
Date dt=new Date();
DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
String currentTime = df.format(dt);
pb.setId(i);
pb.setName("sai");
pb.setEmail("[email protected]");
pb.setAddress("hyderabad");
pb.setCreatedOn(currentTime);
String jsonInString = gson.toJson(pb);
try {
String broker = "tcp://localhost:1883";
String topicName = "test/mqtt";
int qos = 2;
MqttClient mqttClient = new MqttClient(broker,clientId);
MqttConnectOptions connOpts = new MqttConnectOptions();
connOpts.setCleanSession(false);
mqttClient.connect(connOpts);
MqttMessage message = new MqttMessage(jsonInString.getBytes());
message.setQos(qos);
message.setRetained(true);
MqttTopic topic2 = mqttClient.getTopic(topicName);
topic2.publish(message);
mqttClient.disconnect();
} catch (MqttException me) {
System.out.println("reason " + me.getReasonCode() + " - msg "
+ me.getMessage() + "- loc " + me.getLocalizedMessage()
+ " - cause " + me.getCause() + "- exception " + me);
}
}}
public static void main(String[] args) {
Example ex=new Example();
ex.hey();
}}
내
Subscriber.java
public class SubcriberExample implements MqttCallback{
MqttClient client;
public void doDemo() {
try {
client = new MqttClient("tcp://192.168.4.189:1883", "Sending");
client.connect();
client.setCallback(this);
client.subscribe("test/mqtt");
} catch (MqttException e) {
e.printStackTrace();
}
}
public static void main(String args[]){
SubcriberExample se=new SubcriberExample();
se.doDemo();
}
@Override
public void connectionLost(Throwable arg0) {
// TODO Auto-generated method stub
System.out.println("connection lost....");
}
@Override
public void deliveryComplete(IMqttDeliveryToken arg0) {
// TODO Auto-generated method stub
}
@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
// TODO Auto-generated method stub
System.out.println("message is : "+message);
}}
답장을 보내 주셔서 감사합니다.하지만 작동하지 않습니다. 나는 client = new MqttClient를 추가했다. ("tcp : //192.168.4.189 : 1883", "Sending"); \t MqttConnectOptions mqOptions = new MqttConnectOptions(); \t mqOptions.setCleanSession (false); \t client.connect (mqOptions); \t client.subscribe ("test/mqtt"); \t client.setCallback (this); – Sai
나는 가입자가 QoS 1/2로 가입 할 필요가 있다고 생각한다. 그렇지 않으면 QoS가 QoS 0으로 다운 그레이드되고 (클라이언트에서 디폴트로 추측된다) 메시지가 저장되지 않는다. –
자바에서 작은 예제가 있습니까? please – Sai