Quartz Job cant call facebook Service ( Spring Social )

Im trying to call facebook API through my Quartz Job for automatically getting posts, likes, or comments … but I cant. I think I have wrong on SocialConfig.
When i call facebookService from controller, it works normally
Can you help me figure out this problem. Many thanks.

SocialConfigure

@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.TARGET_CLASS)
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();
}

Getting errors

org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘scopedTarget.facebook’: Scope ‘session’ is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException:No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.

I have tried RequestContextListener by adding in web.xml but it doesn’t work.

Calling from Job.

@Override
protected void executeInternal(JobExecutionContext context)
        throws JobExecutionException {
    try {
        ApplicationContextProvider appContext = new ApplicationContextProvider();
        FacebookService facebookService = appContext.getApplicationContext().getBean("facebookService", FacebookService.class);
        System.out.println("!!User: "+facebookService.getFacebookFriends().get(0).getName());
        //runMeTask.printMe();
        System.out.println("!!*****Quartz*****!!");
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

Facebook Service

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();
}


Source: facebook

Leave a Reply