如何监听一个类变量的变化

_回首便他年 2020-12-23 03:31:52
项目环境:springboot
我现在有个类变量,这个类变量是我通过一个3秒执行一次的定时任务进行变化的,也可能不变
如果这个类变量发生变化,我需要去请求一些API,然后往数据库里面插入一些内容

有什么比较好实现的方式吗?
...全文
4227 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
为乐而来 2020-12-25
  • 打赏
  • 举报
回复
在这个表量的set方法上加自定义注解了。 其实没明白,为什么不在定时任务这里面去判断。。这样监听变量去执行不是很推荐。。
_回首便他年 2020-12-25
  • 打赏
  • 举报
回复
引用 2 楼 ninuxGithub 的回复:

package com.xxx.ut;

import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author x
 */
public class Watch {

    private static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);
    private volatile int counter = 0;

    public static void main(String[] args) throws InterruptedException, IOException {
        Watch watch = new Watch();
        startWatch(watch);


        //mock value change: 其他的逻辑修改了变量
        AtomicInteger value = new AtomicInteger(0);
        scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {
            watch.setCounter(value.getAndIncrement());
        }, 3, 2, TimeUnit.SECONDS);


        System.in.read();

    }

    private static void startWatch(Watch watch) {
        new Thread(() -> {
            ThreadLocal<Integer> localCounter = ThreadLocal.withInitial(() -> watch.getCounter());
            for (; ; ) {
                if (localCounter.get() != watch.getCounter()) {
                    System.out.println("counter changed : " + watch.getCounter());
                    localCounter.set(watch.getCounter());
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}

有没有类似于监听器的机制
_回首便他年 2020-12-25
  • 打赏
  • 举报
回复
大佬,有没有类似于监听器的机制,就是监听这个变量的变化来实现
ninuxGithub 2020-12-24
  • 打赏
  • 举报
回复

package com.xxx.ut;

import java.io.IOException;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author x
 */
public class Watch {

    private static ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);
    private volatile int counter = 0;

    public static void main(String[] args) throws InterruptedException, IOException {
        Watch watch = new Watch();
        startWatch(watch);


        //mock value change: 其他的逻辑修改了变量
        AtomicInteger value = new AtomicInteger(0);
        scheduledThreadPoolExecutor.scheduleAtFixedRate(() -> {
            watch.setCounter(value.getAndIncrement());
        }, 3, 2, TimeUnit.SECONDS);


        System.in.read();

    }

    private static void startWatch(Watch watch) {
        new Thread(() -> {
            ThreadLocal<Integer> localCounter = ThreadLocal.withInitial(() -> watch.getCounter());
            for (; ; ) {
                if (localCounter.get() != watch.getCounter()) {
                    System.out.println("counter changed : " + watch.getCounter());
                    localCounter.set(watch.getCounter());
                }
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public int getCounter() {
        return counter;
    }

    public void setCounter(int counter) {
        this.counter = counter;
    }
}

maradona1984 2020-12-23
  • 打赏
  • 举报
回复
这就是为啥需要getter/setter方法了,在setter方法里加上你的逻辑即可. 如果你没有权限修改该类的代码,那大概只能选择字节码增强的方案来添加你的逻辑. 但不管怎么样,都得有个setter方法,或许有更好的,但我不知道.

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧