在Spring中自动装配共享队列的正确方法 [英] Proper way of autowiring shared queue in Spring

查看:194
本文介绍了在Spring中自动装配共享队列的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中实现了一种生产者-消费者模式.一方面,生产者推动实体处理从不同来源接收到的数据,另一方面,我让消费者​​将这些事件从队列中移出并进行处理.

I've got kind of producer-consumer pattern implemented in my application. On one end producer pushes entities to process recieved from different sources, on the other hand I've got consumer which take this events out of the queue and process them.

生产者和使用者都是弹簧豆,并且会自动发现,并且都需要链接到此共享队列.我知道我可以在xml文件或Java配置中定义我的bean,并将此Queue作为构造函数参数或通过setter传递为参数,但是有一种自动导入它的方法.我唯一想到的就是为此队列创建一个包装器,然后注入该包装器:

Both producer and consumer are spring beans and discovered automatically and both require link to this shared Queue. I know that I can define my beans in either xml file or Java configuration and pass this Queue as parameter as constructor argument or via setter, but is there a way to import it automatically. The only idea come to my mind is to create a wrapper for this queue and then inject this wrapper instead :

@Component
public class QueueWrapper {
   private final BlockingQueue<MyObject> sharedQueue = new LinkedBlockingQueue<>();

   public void put(MyObject toPut) {
      sharedQueue.put(toPut);
   }

   public MyObject take() {
      return sharedQueue.take();
   }
}

@Component
public class Producer {
    @Autowire
    private QueueWrapper queue;
    ....
}


@Component
public class Consumer {
    @Autowire
    private QueueWrapper queue;
    ....
}

创建这个包装程序值得吗?我知道@Resource批注,但是我仅将其与列表,映射和集合一起使用,而实际上不知道如何配置资源Java配置文件. Spring文档页面中列表的XML示例:

Does it worth creating this wrapper? I'm aware of @Resource annotation but I've used it with lists, maps and sets only and actually don't know how to configure resource Java config file. XML example of list from Spring documentation page:

<util:list id="emails">
    <value>pechorin@hero.org</value>
    <value>raskolnikov@slums.org</value>
    <value>stavrogin@gov.org</value>
    <value>porfiry@gov.org</value>
</util:list>

然后是Java类:

@Component
public class SomeClass {
   @Resource(name="emails")
   private List<String> emails;
}

是否有一种方法可以在Java配置中将队列创建为此类资源?还是有其他方法可以将共享队列注入不同的bean?

Is there a way to create queue as such resource in java configuration? Or are there another ways to inject a shared queue to different beans?

推荐答案

唐·博特斯坦(Don Bottstein)提出的建议将起作用,您只需要像这样使用它即可.

What Don Bottstein suggested will work, you just have to use it like this.

@Configuration
public class QueueConfig {

    @Bean
    public BlockingQueue<MyObject> blockingQueue() {
        return new LinkedBlockingQueue<>();
    }
}

然后在Producer和Consumer类中执行以下操作:

Then in the class Producer and Consumer do something like this:

@Component
public class Producer {

    @Autowired
    private QueueConfig queueConfig;

    public void produceMyObject(MyObject myObject) {
        queueConfig.blockingQueue.put(myObject);
    }
}

这篇关于在Spring中自动装配共享队列的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆