title: Set Active User to AEM background job throws Exception unexpectedly
body: <p>We have background job running that requires a user to be active. The following way works fine but we have to hard-code the password which is not ideal.</p>
  
  <pre><code>try {
      session = repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
  } catch (RepositoryException ex) {
      log.error("SessionHelper - login issue", ex);
  }
  </code></pre>
  
  <p>We attempt a better way to set active user without setting password as follows:</p>
  
  <pre><code>Map&lt;String, Object&gt; params = new HashMap&lt;String, Object&gt;();
  params.put(ResourceResolverFactory.SUBSERVICE, "theService");
  
  ResourceResolver resolver = null;
  try {
      resolver = resolverFactory.getServiceResourceResolver(params);
  } catch (LoginException e) {
      log.error("LoginException", e);
  }
  
  Session session = resolver.adaptTo(Session.class);
  // Next, create pages and add properties ...
  </code></pre>
  
  <p>We then try to create pages and set properties. This works fine for couple of milliseconds where some pages are created but then throws Exception to indicate session is closed although never closed and the location where exception gets thrown is unpredictable.</p>
  
  <pre><code>javax.jcr.RepositoryException: This session has been closed. See the chained exception for a trace of where the session was closed.
  
  ...
  Caused by: java.lang.Exception: Stack trace of  where session-admin-20077 was originally closed
  </code></pre>
  
  <p>We want to know whether there is any way to set the timeout? Any recommendations appreciated.</p>