声明式服务可以在线程中使用吗?

发布于 2024-10-06 19:05:25 字数 2791 浏览 6 评论 0原文

我有以下代码作为 OSGi 模块。

当它运行时,我收到记录器已设置的消息:

UdpListener > setStoreLog: 'com.mine.logger.internal.storeindb.StoreLog@1c6f579'

但紧接着, run() 函数中的循环表示 storeLog 为空

ERROR > UdpListener > run > storeLog is not available.

你知道可能出了什么问题吗?

难道这是在线程中运行的事实吗?

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 > setStoreLog: '" + this.storeLog.toString() + "'");
    }

    public void unsetStoreLog(IStoreLog value)
    {
        if (this.storeLog == value) {
            this.storeLog = null;
        }
        System.out.println("UdpListener > 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 > UdpListener > run > storeLog is not available.");
                }
                catch (Exception e)
                {
                    System.err.println("ERROR > UdpListener > run > storeLog > Exception: " + e.toString());
                }
            }
        } catch (SocketException e) {
            System.out.println("ERROR > UdpListener > run > SocketException: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("ERROR > UdpListener > run > IOException: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("ERROR > UdpListener > run > Exception: " + e.getMessage());
        }
    }
}

I have the following code as an OSGi module.

When it runs, I get the message that the logger has been set:

UdpListener > setStoreLog: 'com.mine.logger.internal.storeindb.StoreLog@1c6f579'

But immediatly after that, the loop in the run() function says that storeLog is empty

ERROR > UdpListener > run > storeLog is not available.

Any ideas what could be wrong?

Could it be the fact that this is running in a thread?

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 > setStoreLog: '" + this.storeLog.toString() + "'");
    }

    public void unsetStoreLog(IStoreLog value)
    {
        if (this.storeLog == value) {
            this.storeLog = null;
        }
        System.out.println("UdpListener > 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 > UdpListener > run > storeLog is not available.");
                }
                catch (Exception e)
                {
                    System.err.println("ERROR > UdpListener > run > storeLog > Exception: " + e.toString());
                }
            }
        } catch (SocketException e) {
            System.out.println("ERROR > UdpListener > run > SocketException: " + e.getMessage());
        } catch (IOException e) {
            System.out.println("ERROR > UdpListener > run > IOException: " + e.getMessage());
        } catch (Exception e) {
            System.out.println("ERROR > UdpListener > run > Exception: " + e.getMessage());
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-10-13 19:05:25

您的代码不是线程安全的。 storeLog 字段由多个线程读写,无需任何同步。如果您有一个由多个线程读取和写入的可变字段,则必须确保该字段始终能够安全地进行读取和写入访问。我强烈推荐这本优秀的书《Java Concurrency in Practice》http://www.javaconcurrencyinpractice.com/< /a> 适合任何编写 Java 代码的人。

Your code is not thread safe. The storeLog field is read and written by multiple threads without any synchronization. If you have a mutable field that is read and written by more than one thread, you must ensure the field is always safely accessed for both read and write. I highly recommend the excellent book Java Concurrency in Practice http://www.javaconcurrencyinpractice.com/ for anyone writing Java code.

§普罗旺斯的薰衣草 2024-10-13 19:05:25

通过将 storelog 移至单独的类来解决

solved by moving storelog to a seperate class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文