并发问题在ISORequestListener中

huangapple go评论89阅读模式
英文:

Concurrency Issue in ISORequestListener

问题

我正在使用 jpos 库进行事务管理(Spring Boot 项目)。我按照以下方式实现了 ISORequestListener 接口。

@Component
public class MyRequestListener implements ISORequestListener, Configurable {

    protected String var1;
    protected String var2;

    @Override
    public void setConfiguration(Configuration cfg) throws ConfigurationException {
        var1 = cfg.get("var1");
        var2 = cfg.get("var2");
    }

    @Override
    public boolean process(ISOSource source, ISOMsg m) {
        Context ctx = new Context();
        ctx.put("ctx1", var1);
        ctx.put("ctx2", var2);
        return true;
    }
}

在运行 SonarQube(代码分析工具)时,我收到了以下错误信息:

>请使用“@Autowired”、“@Resource”、“@Inject”或“@Value”对全局变量进行注解,或者在 Spring 的 @Component、@Controller、@Service 和 @Repository 类上不要使用这些注解。
>
>Spring 的 @Component、@Controller、@Service 和 @Repository 类默认为单例,这意味着应用程序中只会实例化该类的一个实例。
>
>这样的类可能有一些静态成员,比如记录器,但所有非静态成员应该由 Spring 管理。也就是说,它们应该有这些注解之一:@Resource、@Inject、@Autowired 或 @Value。

这会引起并发问题(或线程问题)吗?还是由 jpos 在内部处理?如果确实会引起问题,正确的实现应该是什么呢?

英文:

I am using jpos library for transaction management(Spring boot project). I am implementing ISORequestListener as below.

@Component
public class MyRequestListner implements ISORequestListener, Configurable {

    protected String var1;
    protected String var2;

    @Override
    public void setConfiguration(Configuration cfg) throws ConfigurationException {
        var1 = cfg.get("var1");
        var2 = cfg.get("var2");
    }

    @Override
    public boolean process(ISOSource source, ISOMsg m) {
        Context ctx = new Context();
        ctx.put("ctx1", var1);
        ctx.put("ctx2", var2);
        return true;
    }
}

On running SonarQube (code analyzing tool), I got the error message as:

>Annotate global variables with "@Autowired", "@Resource", "@Inject", or "@Value", or don’t use on Spring @Component, @Controller, @Service, and @Repository classes.
>
>Spring @Component, @Controller, @Service, and @Repository classes are singletons by default, meaning only one instance of the class is ever instantiated in the application.
>
>Such a class might have a few static members, such as a logger, but all non-static members should be managed by Spring. That is, they should have one of these annotations: @Resource, @Inject, @Autowired or @Value.

Does this cause any concurrency issue(or thread issue) or internally handled by jpos? What could be the correct implementation if it really causes the problem?

答案1

得分: 1

不过,如果您在其自身的Q2微内核内运行jPOS,就没有问题。如果不是这种情况,恐怕您大部分时间都是自行解决。在Q2内部运行非常简单,只需一行代码:new Q2().start();,您可以从任何其他框架中运行它,然后让Q2配置您的jPOS组件。

英文:

Not a problem if you run jPOS within its own Q2 micro-kernel. If that's not the case, I'm afraid you're mostly on your own. Running inside Q2 is extremely simple, it's a one liner: new Q2().start(); that you can run from any other framework, and then let Q2 configure your jPOS components.

huangapple
  • 本文由 发表于 2020年8月26日 21:16:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63598510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定