"heartbeat" approach

Discussion in 'Java' started by bob smith, Feb 1, 2013.

  1. bob smith

    bob smith Guest

    What is the best way to handle a situation where you want a socket to send a "heartbeat" every ten minutes? I was thinking it would be simple to have a single thread do all the "heartbeat" sending.

    However, that means there could be multiple threads writing to one socket. Do I need to do anything special to have multiple threads writing to one socket? Is there a better way?

    Thanks.
    bob smith, Feb 1, 2013
    #1
    1. Advertising

  2. On 2/1/2013 8:11 AM, bob smith wrote:
    > What is the best way to handle a situation where you want a socket to
    > send a "heartbeat" every ten minutes? I was thinking it would be
    > simple to have a single thread do all the "heartbeat" sending.
    >
    > However, that means there could be multiple threads writing to one
    > socket. Do I need to do anything special to have multiple threads
    > writing to one socket? Is there a better way?
    >
    > Thanks.
    >


    Just synchronize on the Socket or the OutputStream. The cost of
    synchronizing in that case would be very low.

    --

    Knute Johnson
    Knute Johnson, Feb 1, 2013
    #2
    1. Advertising

  3. bob smith

    Arne Vajhøj Guest

    On 2/1/2013 11:11 AM, bob smith wrote:
    > What is the best way to handle a situation where you want a socket to
    > send a "heartbeat" every ten minutes? I was thinking it would be
    > simple to have a single thread do all the "heartbeat" sending.
    >
    > However, that means there could be multiple threads writing to one
    > socket. Do I need to do anything special to have multiple threads
    > writing to one socket? Is there a better way?


    You can certainly let the two writing thread synchronize on
    Socket or OutputStream objects to make it thread safe.

    You could also funnel all writes through the same
    thread (a writer thread) via some in memory data structure
    (which you would then need to synchronize on).

    The last option would end up as much more complex
    code from start, but I believe that the design may
    end up being preferable as the solution itself evolves.
    Consider how many places you nee to change if you want to
    switch from TCP to UDP.

    Arne
    Arne Vajhøj, Feb 1, 2013
    #3
  4. On 01.02.2013 23:46, Arne Vajhøj wrote:
    > On 2/1/2013 11:11 AM, bob smith wrote:
    >> What is the best way to handle a situation where you want a socket to
    >> send a "heartbeat" every ten minutes? I was thinking it would be
    >> simple to have a single thread do all the "heartbeat" sending.


    http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html

    >> However, that means there could be multiple threads writing to one
    >> socket. Do I need to do anything special to have multiple threads
    >> writing to one socket? Is there a better way?

    >
    > You can certainly let the two writing thread synchronize on
    > Socket or OutputStream objects to make it thread safe.


    Yes, _some_ form of synchronization is needed.

    > You could also funnel all writes through the same
    > thread (a writer thread) via some in memory data structure
    > (which you would then need to synchronize on).


    The details of course depend on the nature of the heartbeat (e.g. can it
    be omitted if there was regular traffic in the meantime? How much delay
    for the heartbeat is allowed etc.).

    > The last option would end up as much more complex
    > code from start, but I believe that the design may
    > end up being preferable as the solution itself evolves.
    > Consider how many places you nee to change if you want to
    > switch from TCP to UDP.


    We could certainly come up with better solutions if we had more
    information about the scenario.

    Kind regards

    robert

    --
    remember.guy do |as, often| as.you_can - without end
    http://blog.rubybestpractices.com/
    Robert Klemme, Feb 1, 2013
    #4
  5. bob smith

    Roedy Green Guest

    On Fri, 1 Feb 2013 08:11:14 -0800 (PST), bob smith
    <> wrote, quoted or indirectly quoted someone
    who said :

    >However, that means there could be multiple threads

    writing to one socket. Do I need to do anything special to have
    multiple threads writing to one socket? Is there a better way?

    I wrote some proprietary code for a large scale security camera
    monitoring system perhaps 5 years ago that did just that. IIRC
    sockets are happy to have multiple writers. The alternative would be
    to use a queue fed by multiple writers with a single reader. I don't
    recall writing any queue stuff until a year or so ago.

    TCP/IP is completely silent unless you are actively transmitting data,
    unlike many other protocols. So if you can to detect a broken link,
    you need some artificial traffic. That is why he needs a "heartbeat".
    --
    Roedy Green Canadian Mind Products http://mindprod.com
    The first 90% of the code accounts for the first 90% of the development time.
    The remaining 10% of the code accounts for the other 90% of the development
    time.
    ~ Tom Cargill Ninety-ninety Law
    Roedy Green, Feb 1, 2013
    #5
  6. bob smith

    Arne Vajhøj Guest

    On 2/1/2013 6:13 PM, Robert Klemme wrote:
    > On 01.02.2013 23:46, Arne Vajhøj wrote:
    >> On 2/1/2013 11:11 AM, bob smith wrote:
    >>> What is the best way to handle a situation where you want a socket to
    >>> send a "heartbeat" every ten minutes? I was thinking it would be
    >>> simple to have a single thread do all the "heartbeat" sending.

    >
    > http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html


    That could be a very convenient API to get the thread running.

    Arne
    Arne Vajhøj, Feb 1, 2013
    #6
  7. On 02/01/2013 07:13 PM, Robert Klemme wrote:
    > On 01.02.2013 23:46, Arne Vajhøj wrote:
    >> On 2/1/2013 11:11 AM, bob smith wrote:
    >>> What is the best way to handle a situation where you want a socket to
    >>> send a "heartbeat" every ten minutes? I was thinking it would be
    >>> simple to have a single thread do all the "heartbeat" sending.

    >
    > http://docs.oracle.com/javase/6/docs/api/java/util/Timer.html
    >
    >>> However, that means there could be multiple threads writing to one
    >>> socket. Do I need to do anything special to have multiple threads
    >>> writing to one socket? Is there a better way?

    >>
    >> You can certainly let the two writing thread synchronize on
    >> Socket or OutputStream objects to make it thread safe.

    >
    > Yes, _some_ form of synchronization is needed.
    >
    >> You could also funnel all writes through the same
    >> thread (a writer thread) via some in memory data structure
    >> (which you would then need to synchronize on).

    >
    > The details of course depend on the nature of the heartbeat (e.g. can it
    > be omitted if there was regular traffic in the meantime? How much delay
    > for the heartbeat is allowed etc.).
    >
    >> The last option would end up as much more complex
    >> code from start, but I believe that the design may
    >> end up being preferable as the solution itself evolves.
    >> Consider how many places you nee to change if you want to
    >> switch from TCP to UDP.

    >
    > We could certainly come up with better solutions if we had more
    > information about the scenario.
    >
    > Kind regards
    >
    > robert
    >

    Agree with the last. It may be that dealing at the level of sockets is
    not the best way at all. It may also be that there is some discussion
    required about *what* we are calling a heartbeat, whether it is actually
    a "heartbeat" that we are wanting here, or whether external status
    polling is better.

    I myself think of a heartbeat as being a periodic signal initiated by
    the server or application that is being monitored, and generally a push
    to a central monitor.

    I consider that for servers ("server" being loosely defined here as
    *any* standalone app that is taking requests) that the best type of
    status checking in many circumstances is pull by a client. And the best
    type of status check is a request that looks more or less like any other
    kind of request that the server accepts. So, if I was testing a web
    server, I'd send an HTTP request; if email, an SMTP; if messaging I'd
    try to put or get a JMS message.

    AHS
    Arved Sandstrom, Feb 3, 2013
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KavvY
    Replies:
    1
    Views:
    388
  2. valentin tihomirov
    Replies:
    9
    Views:
    2,488
    Ray Andraka
    Apr 15, 2004
  3. Wiener, Norbert
    Replies:
    4
    Views:
    556
    Jerry Avins
    Nov 18, 2004
  4. DavidNorep
    Replies:
    3
    Views:
    699
    Twisted
    Jun 5, 2007
  5. Tristin Davis
    Replies:
    1
    Views:
    99
    Mikael Høilund
    Jun 15, 2008
Loading...

Share This Page