Replies: 7 comments 20 replies
-
The code doesn't work as you might think, the loginFuture is actually resolved in onDisconnected, so it's correct. |
Beta Was this translation helpful? Give feedback.
-
I'm referring to the last master: I understand you say that's correct, but when you call connect().join() the library doesn't return the control, api.connect().join()
System.out.println("hello") //<-- is not printed In any case I din't find any reference to this in the documentation. |
Beta Was this translation helpful? Give feedback.
-
Small correction: |
Beta Was this translation helpful? Give feedback.
-
I was finally able to make it working, it was necessary to add a check for disconnection because after the association a disconnection take place. Please, let me know if this program is using correctly the APIs. I'll keep you posted about the results of the test. This is the latest test program: package it.auties.whatsapp.commandline;
import it.auties.whatsapp.api.QrHandler;
import it.auties.whatsapp.api.WebHistoryLength;
import it.auties.whatsapp.api.Whatsapp;
import it.auties.whatsapp.listener.OnWhatsappLoggedIn;
import it.auties.whatsapp.model.contact.Contact;
import it.auties.whatsapp.model.contact.ContactJid;
import it.auties.whatsapp.model.message.standard.TextMessage;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.LongStream;
public class RunningTest2 {
private static final Random randomGen = new Random();
public static class __OnWhatsappLoggedIn implements OnWhatsappLoggedIn {
@Override
public void onLoggedIn(Whatsapp whatsapp) {
System.out.println("Logged In: " + System.currentTimeMillis());
}
}
public static void main(String args[]){
//ExecutorService executor = Executors.newCachedThreadPool() ;
Whatsapp api = Whatsapp.webBuilder()
.newConnection()
.autodetectListeners(false)
.qrHandler(QrHandler.toTerminal())
//.socketExecutor(executor)
.historyLength(WebHistoryLength.ZERO)
.build() ;
api.addLoggedInListener(new __OnWhatsappLoggedIn());
CompletableFuture<Whatsapp> future = api.connect().thenApply (connectedApi -> {
LongStream randomStream = randomGen.longs(0L, 600L);
randomStream.forEach(r -> {
long sleepMs = r * 1000 ;
System.out.println("Sleeping " + sleepMs + " milliseconds: timestamp(" + System.currentTimeMillis() + ")");
try {
Thread.sleep(sleepMs) ;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if(! connectedApi.isConnected()) {
System.out.println("Reconnecting: " + System.currentTimeMillis() + ")");
connectedApi.reconnect().join() ;
System.out.println("Reconnected: " + System.currentTimeMillis() + ")");
} else if(connectedApi.store().jid() != null) {
long now = System.currentTimeMillis() ;
System.out.println("Trying to send a message: " + now);
ContactJid contactJid = connectedApi.store().jid() ;
Contact contact = Contact.ofJid(contactJid) ;
TextMessage message = TextMessage.builder() // Create a new text message
.title("Test message: " + now)
.text("This is a test message: " + now)
.build() ;
connectedApi.sendMessage(contact, message) ;
}
});
return connectedApi ;
}) ;
future.join();
System.out.println("End");
}
} Thanks! |
Beta Was this translation helpful? Give feedback.
-
So @Auties00 I was able to reproduce the error in about 1h and 10m, using the latest revision of the master branch and the last RunningTest2.java program in the previous comment. I suspect (but I'm not sure) that there are too many thread that do not return the control, i.e. several thread that instead of executing an action and terminate stay alive. Here you have the complete trace of the program:
|
Beta Was this translation helpful? Give feedback.
-
Of course! I ran the program with the breakpoint, let's check tomorrow for the results. |
Beta Was this translation helpful? Give feedback.
-
Hey @Auties00 The program stopped on
About the cached thread pool, just making hypotheses, but I mean that it could tell us something about how the thread are used, for instance, in some place they are never released. |
Beta Was this translation helpful? Give feedback.
-
Hello I found an issue the first time the connect is made, a
new CompletableFuture<>();
is returned that is never completed and that makes theconnect().join()
never returning blocking the thread.The method, in SocketHandler.java is the following:
It make it working changing the function to:
I suspect that something similar happen in the CompletableFuture's in
ReplyHandler.java
andRequest.java
since after some minutes of running the connections stop working and the following exception is thrown (fromRequest.java
->trace
):Anybody have any idea of why after a while the library freezes?
Thanks for any help provided.
ps. I found an error on deserialization that I solved with the following change in
it/auties/whatsapp/binary/PatchType.java
:Beta Was this translation helpful? Give feedback.
All reactions