Hazelcast使用Spring自动装配添加ItemListener。

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

Hazelcast addItemListener with spring autowired

问题

    public static void main(String[] args) {
        HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
        IQueue<Object> queue1 = hazelcastInstance.getQueue("q");
        queue1.addItemListener(new UserListener(), true);
    }
@Component
public class UserListener implements ItemListener<Object> {

    @Autowired
    private UserService userService;

    public void itemAdded(ItemEvent<Object> arg0) {
        if (arg0 != null) {
            try {
                userService.process(arg0);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } else {
            System.out.println("null");
        }
    }

    public void itemRemoved(ItemEvent<Object> item) {
        System.out.println("The item is removed succesfully");
    }
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>hazelcastServer</groupId>
  <artifactId>hazelcastServer</artifactId>
  <version>0.0.1-SNAPSHOT</version> 

  <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.0.1.RELEASE</version>
      <relativePath/>
  </parent>

  <dependencies>
    <dependency>
        <groupId>com.hazelcast</groupId>
        <artifactId>hazelcast</artifactId>
        <version>3.12.9</version>
    </dependency>
    <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
         <groupId>org.springframework</groupId>
         <artifactId>spring-jdbc</artifactId>
         <version>5.0.5.RELEASE</version>
    </dependency>
  </dependencies>
</project>
英文:

I'm trying to create a hazelcast-spring application with listeners but with no success for now. When i try to autowire my UserListener is null.

	HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
	IQueue&lt;Object&gt; queue1 = hazelcastInstance.getQueue(&quot;q&quot;);
	queue1.addItemListener(new UserListener(), true);
    }

This is my main method and it's working ok when i add the item listener with new instance of UserListener class (my listener) but i need to use the spring autowired annotation because in the listener i have service->dao and i want everything to be autowired.Any ideas how can i accomplish this?
This is my whole code:

Main Class:


    public static void main(String[] args) {
    	HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
    	IQueue&lt;Object&gt; queue1 = hazelcastInstance.getQueue(&quot;q&quot;);
    	queue1.addItemListener(new UserListener(), true);
        }

Listener Class:


    @Component
    public class UserListener implements ItemListener&lt;Object&gt; {
    
        @Autowired
        private UserService userService;
    
        public void itemAdded(ItemEvent&lt;Object&gt; arg0) {
    	if (arg0 != null) {
    	    try {
    		userService.process(arg0);
    	    } catch (SQLException e) {
    		e.printStackTrace();
    	    }
    	} else {
    	    System.out.println(&quot;null&quot;);
    	}
       }
    
        public void itemRemoved(ItemEvent&lt;Object&gt; item) {
    	System.out.println(&quot;The item is removed succesfully&quot;);
        }
    }

POM.xml


    &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;hazelcastServer&lt;/groupId&gt;
      &lt;artifactId&gt;hazelcastServer&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt; 
    
    &lt;parent&gt;
    	    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
      	    &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
    	    &lt;version&gt;2.0.1.RELEASE&lt;/version&gt;
     	    &lt;relativePath/&gt;
    	&lt;/parent&gt;
      
      &lt;dependencies&gt;
    &lt;dependency&gt;
        &lt;groupId&gt;com.hazelcast&lt;/groupId&gt;
        &lt;artifactId&gt;hazelcast&lt;/artifactId&gt;
        &lt;version&gt;3.12.9&lt;/version&gt;
    &lt;/dependency&gt;
    &lt;dependency&gt;
    		 &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    		 &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
    	  &lt;/dependency&gt;
     &lt;dependency&gt;
             &lt;groupId&gt;org.springframework&lt;/groupId&gt;
             &lt;artifactId&gt;spring-jdbc&lt;/artifactId&gt;
             &lt;version&gt;5.0.5.RELEASE&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      
      
    &lt;/project&gt;

答案1

得分: 1

这部分代码:

queue1.addItemListener(new UserListener(), true);

会创建一个新的UserListener对象,绕过了Spring,因此任何标记为@Autowired的字段都不会被实例化。

你需要尝试:

UserListener userListener = applicationContext.getBean(UserListener.class);
queue1.addItemListener(userListener, true);

可能值得检查一下userListener对象是否为null,因为Spring组件扫描可能没有找到你的类并创建了一个@Bean,因为组件扫描可能会选择性地扫描哪些包。

英文:

This part of your code

queue1.addItemListener(new UserListener(), true);

creates a new UserListener object bypassing Spring, so any fields annotated as @Autowired don't get instantiated.

You need to try

UserListener userListener = applicationContext.getBean(UserListener.class)
queue1.addItemListener(userListener, true);

It's probably worth checking if userListener object is null, as it might be the Spring component scanning hasn't found you class and created a @Bean, as component scanning can be selective on which packages it scans.

huangapple
  • 本文由 发表于 2020年9月24日 17:51:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/64043901.html
匿名

发表评论

匿名网友

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

确定