Note that there are some explanatory texts on larger screens.

plurals
  1. POCan Declarative Services be used in a Thread?
    primarykey
    data
    text
    <p>I have the following code as an OSGi module.</p> <p>When it runs, I get the message that the logger has been set:</p> <pre><code>UdpListener &gt; setStoreLog: 'com.mine.logger.internal.storeindb.StoreLog@1c6f579' </code></pre> <p>But immediatly after that, the loop in the run() function says that storeLog is empty</p> <pre><code>ERROR &gt; UdpListener &gt; run &gt; storeLog is not available. </code></pre> <p>Any ideas what could be wrong?</p> <p>Could it be the fact that this is running in a thread?</p> <pre><code>package com.mine.logger.internal.udp; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.util.Date; import com.mine.logger.storeindb.IStoreLog; public class UdpListener extends Thread { private int port; private IStoreLog storeLog; public void setStoreLog(IStoreLog value) { this.storeLog = value; System.out.println("UdpListener &gt; setStoreLog: '" + this.storeLog.toString() + "'"); } public void unsetStoreLog(IStoreLog value) { if (this.storeLog == value) { this.storeLog = null; } System.out.println("UdpListener &gt; unsetStoreLog"); } public UdpListener() { // public, no-args constructor needed for Declarative Services !!! } public UdpListener(int port) { this.port = port; } public void run() { startListener(); } private void startListener() { try { // send command DatagramSocket socket = new DatagramSocket(port); while (true) { byte[] b = new byte[1000]; DatagramPacket recvdPacket = new DatagramPacket(b, b.length); socket.receive(recvdPacket); System.out.println("UdpListener: Packet received. " + (new String(b))); try { if (this.storeLog != null) this.storeLog.doStore(new Date(), InetAddress.getByName("0.0.0.0"), port, 1, "UDP", b); else System.err.println("ERROR &gt; UdpListener &gt; run &gt; storeLog is not available."); } catch (Exception e) { System.err.println("ERROR &gt; UdpListener &gt; run &gt; storeLog &gt; Exception: " + e.toString()); } } } catch (SocketException e) { System.out.println("ERROR &gt; UdpListener &gt; run &gt; SocketException: " + e.getMessage()); } catch (IOException e) { System.out.println("ERROR &gt; UdpListener &gt; run &gt; IOException: " + e.getMessage()); } catch (Exception e) { System.out.println("ERROR &gt; UdpListener &gt; run &gt; Exception: " + e.getMessage()); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload