Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mix two sopes? #545

Open
kiril-me opened this issue Feb 9, 2021 · 0 comments
Open

How to mix two sopes? #545

kiril-me opened this issue Feb 9, 2021 · 0 comments

Comments

@kiril-me
Copy link

kiril-me commented Feb 9, 2021

I have a Jersey application and use RequestScope (for connection). Also, I have a separate thread with my custom Scope. How to mix bindings? I usually get an error connection that is not in the request scope.

bindFactory(ConnectionFactory.class)
        .to(Connection.class)
        .proxy(true)
        .proxyForSameScope(false)
        .in(RequestScoped.class);

bindFactory(ConnectionFactory.class)
        .to(Connection.class)
        .proxy(true)
        .proxyForSameScope(false)
        .in(MyScope.class);

....
@Service
@Singleton
public class MyService {
  @Inject
  OperationManager opManager;

   @Inject
   ScopedService service;

  private <T> T runInScope(Callable<T> task) {
    T result;
    try (OperationHandle<MyScope> connectionOp = opManager
        .createOperation(MyScopeImpl.INSTANCE)) {
      connectionOp.resume();
      try {
        result = task.call();
      } catch (Exception e) {
        throw new Exception("Error in the scope", e);
      } finally {
        connectionOp.suspend();
      }
    }
    return result;
  }

 public void doSomething() {
   runInScope(() -> service.getValues());
 }
}
@MyScope
@Service
class ScopedService {
    @Inject
    AnotherService  service; //  connection belongs to requet scope,  however I expect new
}

```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant