我使用@Async
Spring 3.0
我的问题是我无法从该方法看到日志(也没有执行功能),如果我删除@Async
注释,我能够看到日志和功能也被执行!
有没有人遇到过这样的问题?请指导
课程:ContactsCreationUtil
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import <package_name>.entity.ContactDetails;
import <package_name>.contactsdir.rest.IContactsDirRestService;
public class ContactCreationUtil {
private static final Logger logger = LoggerFactory.getLogger(ContactCreationUtil.class);
private IContactsDirRestService contactDirectoryRestService;
public IContactsDirRestService getContactDirectoryRestService() {
return contactDirectoryRestService;
}
public void setContactDirectoryRestService(
IContactsDirRestService contactDirectoryRestService) {
this.contactDirectoryRestService = contactDirectoryRestService;
}
@Async
public void initiateContactCreation(String fromUserId, List<String> toUsers){
System.out.println(Thread.currentThread().getName());
logger.info("init cont creation");
try {
ContactDetails contactDetails = new ContactDetails();
contactDetails.setFromUserId(fromUserId);
contactDetails.setContactEmailIds(toUsers.toArray(new String[toUsers.size()]));
contactDirectoryRestService.addContact(contactDetails);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("init cont creation done");
}
}
module-context.xml
<bean id="contactCreationUtil" class="<package_name>.ContactCreationUtil">
<property name="contactDirectoryRestService" ref="contactDirectoryRestService" />
</bean>
contactDirectoryRestService
是通过OSGI上下文注入的,它是其他OSGI包中的一个函数。
我已经在我的bean中引用了contactCreationUtil
,我将调用异步函数,如下所示
<property name="contactCreationUtil" ref="contactCreationUtil"/>
我在班上创建了以下变量来使用此参考
private ContactCreationUtil contactCreationUtil;
public ContactCreationUtil getContactCreationUtil() {
return contactCreationUtil;
}
public void setContactCreationUtil(ContactCreationUtil contactCreationUtil) {
this.contactCreationUtil = contactCreationUtil;
}
我正在调用这个函数,如下面的
contactCreationUtil.initiateContactCreation(userId, toUsersList);