我试图通过我的 Quartz Job 调用 facebook API 以自动获取朋友、帖子或 newfeeds ......但我无法通过 Job 运行。我认为我对 SocialConfig 有误。当我从控制器调用 facebookService 时,它可以正常工作。你能帮我解决这个问题吗?非常感谢。
社交配置
public class SocialConfig {
@Value("${facebook.clientId}")
private String facebookClientId;
@Value("${facebook.clientSecret}")
private String facebookClientSecret;
@Inject
private DataSource dataSource;
@Bean
public ConnectionFactoryLocator connectionFactoryLocator() {
ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry();
registry.addConnectionFactory(new FacebookConnectionFactory(facebookClientId, facebookClientSecret));
return registry;
}
@Bean
public UsersConnectionRepository usersConnectionRepository() {
JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource,
connectionFactoryLocator(), Encryptors.noOpText());
repository.setConnectionSignUp(new SimpleConnectionSignUp());
return repository;
}
@Bean
@Scope(value="request", proxyMode=ScopedProxyMode.NO)
public ConnectionRepository connectionRepository() {
User user = SecurityContext.getCurrentUser();
return usersConnectionRepository().createConnectionRepository(user.getId());
}
@Bean
@Scope(value="session", proxyMode=ScopedProxyMode.TARGET_CLASS)
public Facebook facebook() {
return connectionRepository().getPrimaryConnection(Facebook.class).getApi();
}
得到错误
org.springframework.beans.factory.BeanCreationException:创建名为“ scopedTarget.facebook”的bean时出错:范围“会话”对于当前线程无效;如果您打算从单例中引用它,请考虑为该 bean 定义一个作用域代理;嵌套异常是 java.lang.IllegalStateException:未找到线程绑定请求:您是指实际 Web 请求之外的请求属性,还是在原始接收线程之外处理请求?如果您实际上是在 Web 请求中操作并且仍然收到此消息,则您的代码可能在 DispatcherServlet/DispatcherPortlet 之外运行:在这种情况下,请使用 RequestContextListener 或 RequestContextFilter 来公开当前请求。
我已经通过添加 web.xml 尝试了 RequestContextListener,但它不起作用。
来自 Job 的调用。
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
try {
ApplicationContextProvider appContext = new ApplicationContextProvider();
FacebookService facebookService = appContext.getApplicationContext().getBean("facebookService", FacebookService.class);
//##It shows error here##
System.out.println("!!User: "+facebookService.getFacebookFriends().get(0).getName());
//runMeTask.printMe();
System.out.println("!!*****Quartz*****!!");
} catch (Exception ex) {
System.out.println(ex);
}
}
脸书服务
public class FacebookService {
private Facebook facebook;
@Inject
public FacebookService(Facebook facebook) {
this.facebook = facebook;
}
}
public Facebook getFacebook() {
return facebook;
}
public void setFacebook(Facebook facebook) {
this.facebook = facebook;
}
public List<Reference> getFacebookFriends() {
// Get Friend List
return getFacebook().friendOperations().getFriends();
}