Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Submit JForex API bug reports in this forum only.
    Submit Converter issues in Converter Issues.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

Errors in threads after IClient.disconnect
 Post subject: Errors in threads after IClient.disconnect Post rating: 1   New post Posted: Tue 17 Jun, 2014, 12:37 

User rating: 6
Joined: Tue 02 Jul, 2013, 16:52
Posts: 38
I am trying to get account information (for now) on my internal webserver. I can get the information I need from the API. But unfortunately the dukascopy thread keeps running and generating errors. How can I kill the thread when stopping the strategy and disconnecting the client?

I get the account information with the following basic strategy (as found in the forum).
package forex.JfUtil;

import com.dukascopy.api.*;
import forex.model.Consts;

public class MyFirstStrategy implements IStrategy{
    private IAccount myAccount = null;
    private IContext myContext = null;

  @Override
    public void onStart(IContext context) throws JFException {
        myContext = context;
        myAccount = myContext.getAccount();
        Consts.LOG.debug("Balance: " + myAccount.getBalance());
    }
  @Override
    public void onStop() throws JFException {
        Consts.LOG.debug("Strategy successfully stopped!");
    }
}
I keep the other methods empty.

Then I launch the strategy from a servlet with the following code:
import com.dukascopy.api.system.ClientFactory;
import com.dukascopy.api.system.IClient;
import forex.JfUtil.MyFirstStrategy;
import forex.model.Consts;
import javax.servlet.*;
import java.io.IOException;
import java.net.NoRouteToHostException;

@WebServlet("/BalanceFromDuka")
public class BalanceFromDuka extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        IClient client = null;
        try {
            client = ClientFactory.getDefaultInstance();
            Consts.LOG.info("Connecting & testing ...");
            client.connect("https://www.dukascopy.com/client/demo/jclient/jforex.jnlp", "", "");
            //wait for it to connect
            int i = 10; //wait max ten seconds
            while (i > 0 && !client.isConnected()) {
                Thread.sleep(1000);
                i--;
            }
            if (!client.isConnected()) {
                Consts.LOG.error("Failed to connect Dukascopy servers");
            } else {
                final long strategyId = client.startStrategy(new MyFirstStrategy());
                client.stopStrategy(strategyId);
            }
         } catch (Exception e) {
            Consts.LOG.error("EXCEPTION", e);
        } finally {
            if (client!=null) {
                client.disconnect();
                Consts.LOG.debug("Disconnected from Dukascopy servers");
            }
        }
        RequestDispatcher view = request.getRequestDispatcher("/DashServlet.do");
        view.forward(request, response);
    }
}
I would expect that dukascopy threads would stop after the strategy ends and the client is disconnected. My app logs are:
18:58:22 [     http-bio-7070-exec-4] -  INFO -      forex.web.BalanceFromDuka: 39 - Connected to Dukascopy servers
18:58:25 [ Strategy MyFirstStrategy] - DEBUG - forex.dukaTools.MyFirstStrategy: 38 - Balance: xxxxxx
18:58:25 [ Strategy MyFirstStrategy] - DEBUG - forex.dukaTools.MyFirstStrategy:113 - Strategy successfully stopped!
18:58:26 [     http-bio-7070-exec-4] - DEBUG -      forex.web.BalanceFromDuka: 50 - Disconnected from Dukascopy servers

Unfortunately, I keep getting the following errors every minute in my logs:
18:59:19 [CriteriaIsolationExecutorThreadsMonitor] - ERROR - dukascopy.transport.client.CriteriaThread: 48 - Found not fired event, with wait time > 2 sec: {"userId":"...
19:00:19 [CriteriaIsolationExecutorThreadsMonitor] - ERROR - dukascopy.transport.client.CriteriaThread: 48 - Found not fired event, with wait time > 2 sec: {"userId":"...
19:01:19 [CriteriaIsolationExecutorThreadsMonitor] - ERROR - dukascopy.transport.client.CriteriaThread: 48 - Found not fired event, with wait time > 2 sec: {"userId":"...
etc....
19:40:19 [CriteriaIsolationExecutorThreadsMonitor] - ERROR - dukascopy.transport.client.CriteriaThread: 48 - Found not fired event, with wait time > 2 sec: {"userId":"...
If I launch the strategy a second time to get additional information, it will create a second thread which will also remain active and will also generate errors. Therefore the number of threads keeps increasing on the webserver :cry: Even If I stop my webapp, these threads are still running.

I realized there are more threads still running after disconnecting iClient (although it seems only CriteriaIsolationExecutorThreadsMonitor creates problems):
SEVERE: The web application [/forex-beta] appears to have started a thread named [CriteriaIsolationExecutorThreadsMonitor] but has failed to stop it. This is very likely to create a memory leak.
SEVERE: The web application [/forex-beta] appears to have started a thread named [ActivityLogger] but has failed to stop it. This is very likely to create a memory leak.
SEVERE: The web application [/forex-beta] appears to have started a thread named [Mina_Thread_1] but has failed to stop it. This is very likely to create a memory leak.
SEVERE: The web application [/forex-beta] appears to have started a thread named [OrderedThreadPoolExecutor CleanUp] but has failed to stop it. This is very likely to create a memory leak.
SEVERE: The web application [/forex-beta] appears to have started a thread named [LiveCurrencyMarketProcessingThread 1] but has failed to stop it. This is very likely to create a memory leak.

The CriteriaIsolationExecutorThreadsMonitor thread is problematic because it keeps generating errors in the log and the number of these threads increases each time I launch the strategy to get data.
So am I doing something wrong when ending the strategy and disconnecting the client? Should I wait for other threads to end before? How can I get rid of the dukascopy.transport.client.CriteriaThread errors and stop the number of thread to increase?

I am using DDS2-jClient-JForex:2.36

Thank you in advance for any help


 
 Post subject: Re: Errors in threads after IClient.stopStrategy and IClient.disconnect Post rating: 0   New post Posted: Wed 18 Jun, 2014, 17:52 

User rating: 6
Joined: Tue 02 Jul, 2013, 16:52
Posts: 38
To avoid "asynchronous issues", I changed my code to :
1 - stop the strategy directly at the end of MyFirstStrategy.Onstart with myContext.stop()
2 - implement client.setSystemListener(new ISystemListener()... to disconnect the client only in the onStop method. Therefore I am sure that the strategy has actually been stopped before I disconnect.

Unfortunately, the problem is still there
- the Error "transport.client.CriteriaThread - Found not fired event, with wait time > 2 sec" still appears every minute for most CriteriaIsolationExecutorThreadsMonitor threads (but actually not for all of them). Right now, I have 5 CriteriaIsolationExecutorThreadsMonitor threads running and 3 of these threads are generating the error (every minute for each thread).
- the number of CriteriaIsolationExecutorThreadsMonitor threads increases after each StartStrategy (ie each time I launch the servlet to get account information)

Edit: actually it looks like the OnDisconnect() in client.setSystemListener(new ISystemListener()... is never executed. I can't find any logical explanation...


 
 Post subject: Re: Errors in threads after IClient.stopStrategy and IClient.disconnect Post rating: 1   New post Posted: Thu 19 Jun, 2014, 06:08 
User avatar

User rating: 98
Joined: Mon 23 Jul, 2012, 02:02
Posts: 656
Location: United States, Durham, NC
First of all, I wouldn't attempt to design things this way,
using a servlet and a transient login, but...

At the end of your onStart you should probably call

context.stop();

...if you want to terminate the IStrategy instance but
there's probably more to debugging a design like
this, than that observation.

.startStrategy isn't a synchronous blocking call so you'll need to
do some thread waiting, and a whole lot more issues, I'd
think.....

HyperScalper


 
 Post subject: Re: Errors in threads after IClient.stopStrategy and IClient.disconnect Post rating: 0   New post Posted: Thu 19 Jun, 2014, 07:47 

User rating: 6
Joined: Tue 02 Jul, 2013, 16:52
Posts: 38
Thank you for your answer.
Because it's asynchronous, I already changed my code to used context.stop(); and client.setSystemListener(new ISystemListener() { (.... (as mentionned in post #2).
    
public class BalanceFromDuka extends HttpServlet {

    /** logger.   */
    private static final Logger LOGGER = LoggerFactory.getLogger(BalanceFromDuka.class);

    protected final void doGet(final HttpServletRequest request, final HttpServletResponse response)
            throws ServletException, IOException {
         try {
            final IClient client = ClientFactory.getDefaultInstance();
            client.setSystemListener(new ISystemListener() {

                @Override
                public void onStart(long processId) {
                    LOGGER.info("Strategy started: " + processId);
                }

                /**
                 * Called on the strategy stop.
                 * @param processId id of the strategy stopped
                 */
                @Override
                public void onStop(long processId) {
                    LOGGER.info("Strategy: stopped({})!",processId);
                    if (client.getStartedStrategies().size() == 0) {
                        client.disconnect();
                        LOGGER.debug("Trying to disconnect from Dukascopy servers");
                    }
                }

                /** Called on successful connect.   */
                @Override
                public void onConnect() {  }

                /** Called on disconnect.         */
                @Override
                public void onDisconnect() {
                    LOGGER.debug("Disconnected from Dukascopy servers");
                }
            });

            LOGGER.info("Connecting to Dukascopy servers ...");
            client.connect(Consts.DUKAURL, Consts.DUKALOGIN, Consts.DUKAPWD);
            //wait for it to connect
            int i = 10; //wait max ten seconds
            while (i > 0 && !client.isConnected()) {
                Thread.sleep(1000);
                i--;
            }
            if (!client.isConnected())  {
                LOGGER.error("Failed to connect Dukascopy servers");
            } else {
                LOGGER.info("Connected  to Dukascopy servers");
                client.startStrategy(new MyStrategy1());
            }
        } catch (Exception e) {
            LOGGER.error("EXCEPTION", e);
        }
   }
}
public class MyStrategy1 implements IStrategy{
    /**  IAccount object.   */
    private IAccount myAccount = null;
    private IContext myContext = null;
    /** logger. */
    private static final Logger LOGGER = LoggerFactory.getLogger(MyStrategy1.class);

    @Override
    public void onStart(IContext context) throws JFException {
        myContext = context;
        myAccount = myContext.getAccount();
        LOGGER.debug("Balance: {}",myAccount.getBalance());
        myContext.stop(); //stop the strategy
   }
But the errors are still there (see post #2), and the OnDisconnect() is never executed.
Besides these problems, the asynchronous process seems fine in my logs
18:54:14 [http-bio-7070-exec-2] -  INFO -   BalanceFromDuka: 71 - Connecting to Dukascopy servers ...
18:54:20 [http-bio-7070-exec-2] -  INFO -   BalanceFromDuka: 82 - Connected  to Dukascopy servers
18:54:22 [Strategy MyStrategy1] - DEBUG -       MyStrategy1: 36 - Balance: xxxx.xxx
18:54:23 [Strategy MyStrategy1] - DEBUG -       MyStrategy1:111 - Strategy: stopping...
18:54:23 [Strategy MyStrategy1] -  INFO - BalanceFromDuka$1: 53 - Strategy: stopped(52)!
18:54:23 [http-bio-7070-exec-2] - DEBUG -         GetDBdata: 59 - some other stuff...
18:54:23 [http-bio-7070-exec-2] - DEBUG -         GetDBdata: 59 - some other stuff...
18:54:24 [Strategy MyStrategy1] - DEBUG - BalanceFromDuka$1: 56 - Trying to disconnect from Dukascopy servers
19:03:13 [http-bio-7070-exec-8] -  INFO -             Trade:155 - some other stuff...
19:04:40 [ttp-bio-7070-exec-10] -  INFO -          Position:650 - some other stuff...
Indeed, the alternative solution would be to use a permanent connection to the server. I thought this would not make sense to keep it open 24*7 to mostly get account and orders data from time to time (at least for now).


 
 Post subject: Re: Errors in threads after IClient.disconnect Post rating: 0   New post Posted: Thu 19 Jun, 2014, 13:53 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Why would you need to disconnect on strategy stop if you want to keep working in the same account after the strategy stop? Simply remain connected as long as you are working the same account.


 
 Post subject: Re: Errors in threads after IClient.disconnect Post rating: 0   New post Posted: Fri 20 Jun, 2014, 09:56 

User rating: 6
Joined: Tue 02 Jul, 2013, 16:52
Posts: 38
Ok, for now I remain connected permanently.
However:
- I need to disconnect and unload the thread before undeploying or stopping the webapp. To do that, I use disconnect() method. But it doesn't unload all threads. That means I must stop the web server each time I need to undeploy the webapp (basically each time I update my code with a new version) to avoid memory leaks and increase in # of threads. This is a personal internal server, but still, this is not the only webapp running on it, so this is annoying.
- In the future, I will need to change account (from demo to live and vice et versa) without having to stop the webserver.

So yes, right now I can live with this by remaining connected and stopping the webserver each time I have new code; but it would be really nice if in the future it would be possible to fix this issue :-)

Thank you


 
 Post subject: Re: Errors in threads after IClient.disconnect Post rating: 0   New post Posted: Fri 20 Jun, 2014, 15:32 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
fxbird wrote:
However:
- I need to disconnect and unload the thread before undeploying or stopping the webapp. To do that, I use disconnect() method. But it doesn't unload all threads. That means I must stop the web server each time I need to undeploy the webapp (basically each time I update my code with a new version) to avoid memory leaks and increase in # of threads. This is a personal internal server, but still, this is not the only webapp running on it, so this is annoying.
- In the future, I will need to change account (from demo to live and vice et versa) without having to stop the webserver.
We are going to have a look if there are any additional disposal procedures to be added on the client disconnect.


 
 Post subject: Re: Errors in threads after IClient.disconnect Post rating: 0   New post Posted: Mon 03 May, 2021, 19:55 

User rating: 2
Joined: Fri 27 Jul, 2018, 13:46
Posts: 14
Location: ItalyItaly
Hello,
I have the same problem using servlet.
Have you news/suggestions about this issue?
Any workaround should be a better solution then restarting the webserver.

Thank you,
Fabrizio


 
 Post subject: Re: Errors in threads after IClient.disconnect Post rating: 0   New post Posted: Wed 05 May, 2021, 09:23 

User rating: 6
Joined: Tue 02 Jul, 2013, 16:52
Posts: 38
That was a long time ago; now I connect when starting the servlet server, and disconnect when stopping it.


 
 Post subject: Re: Errors in threads after IClient.disconnect Post rating: 0   New post Posted: Thu 06 May, 2021, 11:20 

User rating: 2
Joined: Fri 27 Jul, 2018, 13:46
Posts: 14
Location: ItalyItaly
Hello,
me also make the connect call when servlet starts and disconnect when I stop the webapp (not the server).
The problem still remains, I have the following threads not properly ended:

- ActivityLogger
- TimeChangeTrackerThread
- some others processes regarding netty

Do you have a fully shutdown of the client connection whitout memory leaks?

Thank you,
Fabrizio


 
 Post subject: Re: Errors in threads after IClient.disconnect Post rating: 0   New post Posted: Tue 11 May, 2021, 17:28 

User rating: 6
Joined: Tue 02 Jul, 2013, 16:52
Posts: 38
Not that I can remember, but I would have to check. My server is not easily accessible anymore, so not so easy to check

ftasso wrote:
Hello,
me also make the connect call when servlet starts and disconnect when I stop the webapp (not the server).
The problem still remains, I have the following threads not properly ended:

- ActivityLogger
- TimeChangeTrackerThread
- some others processes regarding netty

Do you have a fully shutdown of the client connection whitout memory leaks?

Thank you,
Fabrizio


 

Jump to:  

  © 1998-2024 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com