Skip to content
This repository was archived by the owner on Feb 9, 2019. It is now read-only.
This repository was archived by the owner on Feb 9, 2019. It is now read-only.

JedisPool is null when trying to inject in another class than the controllers #157

Description

@johdah

I am able to inject the JedisPool in my controllers but not in models, daos or other classes. An example is in my UserDAO. Isn't this supported or why does the exact same code work in the Application controller?

...
import javax.inject.Inject;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class UserDAO extends AbstractDAO<User, User_> {
    @Inject JedisPool jedisPool;

    public UserDAO() {
        super(User.class, User_.class);
    }

    /**
     * @return an instance of {@link UserDAO}.
     */
    public static UserDAO newInstance() {
        return new UserDAO();
    }

    ...

    // -- Redis

    /**
     * Get the last time the user accessed the service.
     *
     * @param user the {@link models.User}
     * @return the last time as a {@link org.joda.time.DateTime}
     */
    public DateTime getLastAccess(User user) {
        if(jedisPool == null) {
            Logger.error("Redis is not available");
            return null;
        }

        Jedis j = jedisPool.getResource();
        DateTime dateTime = null;

        try {
            String dtString = j.hget("userLastAccess", user.id.toString());
            dateTime = new DateTime(Long.valueOf(dtString));
        } catch(NumberFormatException ex) {
            dateTime = null;
        } finally {
            jedisPool.returnResource(j);
        }

        return dateTime;
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions