Multithreading – how do I call runnable using the spring framework?

I have a service that needs to call the runnable class

Here are the lines of code used in my service

@Autowired
private LinkBrc2MemberProfile brcTask;                

// Background Task.
SimpleAsyncTaskExecutor sate = new SimpleAsyncTaskExecutor();
sate.createThread(new LinkBrc2MemberProfile(user));

This is my runnable class

@Service
public class LinkBrc2MemberProfile implements Runnable {

    private final Logger log = LoggerFactory.getLogger(LinkBrc2MemberProfile.class);

    @Autowired
    private LoyaltyDao dao;

    private Member member;

    public LinkBrc2MemberProfile() {
        super();
    }

    public LinkBrc2MemberProfile(Member member) {
        this.member = member;
    }

    public void run() {
        log.debug("*** Member User Name: " + member.getString("USER_NAME"));
        String emailAddress = member.getString("USER_NAME");
        Map<String,Object> map = dao.findBrcByEmailAddress( emailAddress );
        log.debug("==========================================================");

        if( ! map.isEmpty() ) {
            try {
                //a.CUSTOMER_ID,a.EMAIL_ADDRESS,b.card_no
                String customerId    = (String) map.get("CUSTOMER_ID");
                String brcCardNumber = (String) map.get("CARD_NO");

                log.debug("\ncustomerId: " + customerId + " brcCardNumber: " + brcCardNumber);
                if(!brcCardNumber.equals("")) {
                    // Add the Be Rewarded Card.
                    HashMap<String,String> userAttributes = new HashMap<String,String>();
                    String brcNumber = member.getString("BREWARDED_CARD_NO");
                    if (brcNumber.equals("")) {
                       userAttributes.put("BREWARDED_CARD_NO",brcCardNumber);
                       try {
                          member.putAll(userAttributes);
                       } catch (Exception e) {
                          String errorMessage = "Unable to save user's BRC information due to: " + e.getMessage();
                          log.error("{}",errorMessage);
                       }
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                e.printStackTrace();
            }
        }
    }

I don't see any errors in the log, but at the same time it doesn't seem to call the runnable class Where did I miss the comment? Is there any good example to point out that I found that I only use XML files to configure the runnable class I want to use annotations Thank you in advance

I have updated my service to do the following

Please help, my Dao is null, so it seems that @ Autowired in my runnable class is not connected to it

I have added the following beans to my bean config XML file

<bean id="brcType" class="com.ws.ocp.service.LinkBrc2MemberProfile" scope="prototype"/>

I deleted the @ Autowired annotation and added the following to my service class

ClassPathResource rsrc = new ClassPathResource("bean-config.xml");
Xmlbeanfactory factory = new Xmlbeanfactory(rsrc);
LinkBrc2MemberProfile brcTask = (LinkBrc2MemberProfile) factory.getBean("brcType");                

SimpleAsyncTaskExecutor sate = new SimpleAsyncTaskExecutor();               
// Set Member attribute
brcTask.setMember(user);
// Executer
sate.execute(brcTask);

Why is my Dao still null?

Solution

Runnable throws a NullPointerException because you create it yourself (using the new operator), rather than letting spring create it This obviously means that the Dao attribute of auto assembly will not be auto assembled, which will be called when Dao Findbrcbyemailaddress (...) causes NPE

You should get the runnable instance from the bean factory (as a prototype), set its member properties, and then submit it to the executor

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>