Quantcast
Channel: Active questions tagged redis+java - Stack Overflow
Viewing all 2203 articles
Browse latest View live

Create Repository using redis in SpringBoot App

$
0
0

I have a object:

@Data@AllArgsConstructorpublic class ResultGeoObjectDto {   private String addressLine;   private String location;   private double latitude;   private double longitude;}

I created a service, which work with my Object and Redis:

@Servicepublic class RedisService {private final RedisTemplate<String, List<ResultGeoObjectDto>> redisTemplate;@Autowiredpublic RedisService(RedisTemplate<String, List<ResultGeoObjectDto>> redisTemplate) {    this.redisTemplate = redisTemplate;}public void setValue(String key, List<ResultGeoObjectDto> value) {    redisTemplate.opsForList().leftPush(key, value);}public List<ResultGeoObjectDto> getGeoObjectByKey(String key) {    return redisTemplate.opsForList().range(key, -1, -1).get(0);}public boolean objectExistInRedisStore(String key) {    return redisTemplate.hasKey(key);}

}

This works great, but many examples use a Repository pattern. Can you tell me how to make a repository?

For example here using a static key, and I have it dynamically formed. And also I have a List of objects, instead of one. I can not understand how I need to do the right architecture.


Radisson version to work with Spring 4.1.4?

$
0
0

I have been trying to find out the right version of redisson to work with spring 4.1.4, here is pom

enter image description here

i am getting error

enter image description here

If i am upgrading the spring-context to 5.2.2.RELEASE it throws error with spring-core dependency, i even excluded spring-core from spring-context(5.2.2.RELEASE) so it throws error with spring-beans dependency.

So would like to know what's the correct version of redisson for spring 4.1.4 ?

Disable spring session with redis in integrationtests

$
0
0

I am using spring session with redis, but I want to disable it while doing tests. My class is annotated:

@ActiveProfiles("integrationtests")

and my application-integrationtests.tml file contains:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration

but it still fails with:

Caused by: org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool

tests work if I turn redis-server on, but ofcourse I dont want to keep it that way ; )

//update

ive been trying with

@SpringBootTest(classes = {Application.class})@ActiveProfiles("integrationtests")

and Application.class with excluded Redis:

@SpringBootApplication(exclude={SessionAutoConfiguration.class, RedisAutoConfiguration.class, RedisHttpSessionConfiguration.class})public class Application {public static void main(String[] args) throws Exception {    SpringApplication.run(Application.class, args);}}

but it fails with:

Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]

spring autoconfigure debug see that Ive excluded this class, but it has no effect:

Exclusions:-----------org.springframework.boot.autoconfigure.session.SessionAutoConfigurationorg.springframework.boot.autoconfigure.data.redis.RedisAutoConfigurationorg.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration

Spring JpaSpecificationExecutor no property findAll found for type Alarm

$
0
0

I use Redis to store my application's alarms with entity as below:

@RedisHash("alarm")public class Alarm implements Serializable{    @Id    @GeneratedValue(strategy = GenerationType.SEQUENCE)    private Long id;    @Indexed    private String name;    @Indexed    private String createdAt;    @Indexed    private String createdBy;    @Indexed    private String clearAt;    @Indexed    private String clearBy;}

Data source configuration is like below:

@Configuration@EnableRedisRepositories(basePackages = "com.app.demo.redis.repository")public class RedisDataSourceConfiguration {@Autowiredprivate RedisProperties redisProperties;@Beanpublic JedisConnectionFactory jedisConnectionFactory(){    RedisStandaloneConfiguration redisConfiguration = new RedisStandaloneConfiguration();    return new JedisConnectionFactory();    }@Beanpublic RedisTemplate<Object, Object> redisTemplate(){    RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>();    template.setConnectionFactory(jedisConnectionFactory());    }}

And this is repository:

@Repositorypublic interface AlarmRedisRepository extends JpaRepository<Alarm, Long>, JpaSpecificationExecutor<Alarm>{}

I used Specification to customize the search:

public class AlarmSpecification implements Specification<Alarm>{    private Alarm filter;    public AlarmSpecification(Alarm alarm){        this.alarm = alarm;    }    @Override    public Predicate toPredicate(Root<Alarm> root, CriteriaQuery<?> query, CriteriaBuilder builder){            // This is where I added the filter            Predicate predicate = builder.disjunction();            return predicate;    }}

In my @Service:

@Autowiredprivate AlarmRedisRepository repo;Pageable pageable = .....Alarm alarm = .....Specification<Alarm> specification = new AlarmSpecification(alarm);Page<Alarm> page = repo.findAll(specification, pageable);

it returns the error as the titlte. Anybody who used to work with Redis and Specification can help me !!! Thank in advanced

Is there any event I can subscribe to when a redis db is flushed #redis

$
0
0

I have an issue in my application(Java,Spring web application) where some of the objects are places in redis, and my Ops team does a FLUSHALL on the db without consent and some of my functionality breaks due to FLUSH. So is there an event that I can subscribe to whenever a redis FLUSHALL is executed so that I can rebuild some of my objects and store them in redis again.

How to pass own executors to the redis lettuce library?

$
0
0

I have below code to get the data from Redis asynchronously. By default get() call in lettuce library uses nio-event thread pool.

Code 1:

StatefulRedisConnection<String, String> connection = redisClient.connect();RedisAsyncCommands<String, String> command = connection.async();CompletionStage<String> result = command.get(id)        .thenAccept(code ->                      logger.log(Level.INFO, "Thread Id "+ Thread.currentThread().getName());                     //Sample code to print thread ID

Thread Id printed is lettuce-nioEventLoop-6-2.

Code 2:

CompletionStage<String> result = command.get(id)            .thenAcceptAsync(code -> {                         logger.log(Level.INFO, "Thread Id "+ Thread.currentThread().getName());              //my original code}, executors);

Thread Id printed is pool-1-thread-1.

My questions:

  1. Is there a way to pass my executors?
  2. Is it recommended approach to use nio-event thread pool to get(using get() call) the data from redis?

Lettuce version: 5.2.2.RELEASE

thanks, Ashok

How to design "anonymous" functionality in the backend - good design patterns

$
0
0

When developing the notification center, we want to push newly generated notifications to the user. (using WebSocket, but that is unrelated to the question.) However, the notifications is generated very fast, so we want to use the throttle method (a term in Lodash, or see this visualization). We will throttle the "notification generated" event.

A visualization: X-axis is time. The first blue row is the generated notifications, not necessarily same time interval of course. The red row is the throttled events. (ignore green)

enter image description here

The problem is how to implement this. We have a naive solution as follows:

  1. Whenever a notification is generated, enqueue a "delayed task" into Kafka with delay=throttleTime.
  2. When Kafka deliver a delayed message to us, check the lastActionTimestamp in Redis. If currentTimestamp - lastActionTimestamp < throttleTime, it means that this message should be throw away. Otherwise, do action, and update the lastActionTimestamp in Redis to be the current time.

I think it is a little bit complicated. So is there any better or simpler methods? Or are there any libraries? Thanks!


Disclaimer: It is not the often-seen "throttling" - not wanting to rate-limit some user actions or microservice calls.

How to fetch cached value using redisson client

$
0
0

I wanted to fetch cached(@Cachable) value using redisson client but it throws error.

i have used below code for caching

    @Cacheable(value = "fruits", key = "#id")    public Fruit getFruitById(int id) {    // get fruit by id    CriteriaBuilder builder = em.getCriteriaBuilder();    CriteriaQuery<Fruit> query = builder.createQuery(Fruit.class);    Root<Fruit> root = query.from(Fruit.class);    query.select(root);    query.where(builder.equal(root.get("id"), id));    TypedQuery<Fruit> fruitQuery = em.createQuery(query);    return fruitQuery.getSingleResult();}

i've used below code fetching that cached data

    RBucket<Fruit> bucket = client.getBucket("fruits::1");    Fruit fruit = bucket.get();

even though data exist for that key(fruit::1) it throws below error

2020-04-21 23:45:43.408 ERROR 12652 --- [isson-netty-5-6] o.r.client.handler.CommandDecoder        : Unable to decode data. channel: [id: 0xc3f4bf59, L:/127.0.0.1:56641 - R:localhost/127.0.0.1:6379], reply: ReplayingDecoderByteBuf(ridx=110, widx=110), command: (GET), params: [fruits::1]java.io.IOException: java.lang.RuntimeException: unknown object tag -84at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:247) ~[fst-2.57.jar:na]at org.redisson.codec.FstCodec$1.decode(FstCodec.java:250) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:368) [redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196) [redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134) [redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104) [redisson-3.12.4.jar:3.12.4]at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:498) [netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) [netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) [netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) [netty-common-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [netty-common-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-common-4.1.48.Final.jar:4.1.48.Final]at java.lang.Thread.run(Unknown Source) [na:1.8.0_231]Caused by: java.lang.RuntimeException: unknown object tag -84at org.nustaq.serialization.FSTObjectInput.instantiateSpecialTag(FSTObjectInput.java:434) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:364) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:331) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:311) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:245) ~[fst-2.57.jar:na]... 24 common frames omitted2020-04-21 23:45:43.410  WARN 12652 --- [isson-netty-5-6] io.netty.channel.DefaultChannelPipeline  : An exceptionCaught() event was fired, and it reached at the tail of the pipeline. It usually means the last handler in the pipeline did not handle the exception.io.netty.handler.codec.DecoderException: java.io.IOException: java.lang.RuntimeException: unknown object tag -84at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:421) ~[netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) ~[netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) [netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) [netty-common-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) [netty-common-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) [netty-common-4.1.48.Final.jar:4.1.48.Final]at java.lang.Thread.run(Unknown Source) [na:1.8.0_231]Caused by: java.io.IOException: java.lang.RuntimeException: unknown object tag -84at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:247) ~[fst-2.57.jar:na]at org.redisson.codec.FstCodec$1.decode(FstCodec.java:250) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:368) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104) ~[redisson-3.12.4.jar:3.12.4]at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:498) ~[netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) ~[netty-codec-4.1.48.Final.jar:4.1.48.Final]... 17 common frames omittedCaused by: java.lang.RuntimeException: unknown object tag -84at org.nustaq.serialization.FSTObjectInput.instantiateSpecialTag(FSTObjectInput.java:434) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:364) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:331) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:311) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:245) ~[fst-2.57.jar:na]... 24 common frames omitted2020-04-21 23:45:43.413 ERROR 12652 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.redisson.client.RedisException: Unexpected exception while processing command] with root causejava.lang.RuntimeException: unknown object tag -84at org.nustaq.serialization.FSTObjectInput.instantiateSpecialTag(FSTObjectInput.java:434) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObjectWithHeader(FSTObjectInput.java:364) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObjectInternal(FSTObjectInput.java:331) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:311) ~[fst-2.57.jar:na]at org.nustaq.serialization.FSTObjectInput.readObject(FSTObjectInput.java:245) ~[fst-2.57.jar:na]at org.redisson.codec.FstCodec$1.decode(FstCodec.java:250) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:368) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decodeCommand(CommandDecoder.java:196) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:134) ~[redisson-3.12.4.jar:3.12.4]at org.redisson.client.handler.CommandDecoder.decode(CommandDecoder.java:104) ~[redisson-3.12.4.jar:3.12.4]at io.netty.handler.codec.ByteToMessageDecoder.decodeRemovalReentryProtection(ByteToMessageDecoder.java:498) ~[netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.handler.codec.ReplayingDecoder.callDecode(ReplayingDecoder.java:366) ~[netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:276) ~[netty-codec-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:357) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1410) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:379) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:365) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:919) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:714) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:650) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:576) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:493) ~[netty-transport-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:989) ~[netty-common-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.48.Final.jar:4.1.48.Final]at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.48.Final.jar:4.1.48.Final]at java.lang.Thread.run(Unknown Source) [na:1.8.0_231]

It'd be helpful if anyone let me know the solution for this problem.. i want to know whether it's possible or not and if possible please explain with code


Which Java Redis clients would you recommend to work with JSON (RedisJSON module)?

$
0
0

I thought I would use Jedis client. But since I have found out how to work with JSON using this client, I am not quite sure that it is a good idea. JReJSON, from Redis Labs themselves, is mentioned in module page on the Redis website. Is it the best option to use or there are some more convenient clients?

How to force stop/kill Spring Redis embedded server programmatically

$
0
0

I am using Redis embedded server for my integrations tests and I noticed that using the RedisServer.stop() method is not really stoping the mock server. Any idea how to force kill it in my test cases?

Dependency:

<!-- Redis test mock server  --><dependency><groupId>it.ozimov</groupId><artifactId>embedded-redis</artifactId><version>0.7.2</version><scope>test</scope></dependency>

Cache integration test:

@SpringBootTest@ActiveProfiles("test")@ExtendWith({SpringExtension.class, RestDocumentationExtension.class})class CacheServiceImplTest {    private RedisServer redisServer;    @MockBean    private StoreRepository repo;    @Autowired    public ObjectMapper objectMapper;    @Autowired    private CacheService cacheService;    @PostConstruct    public void postConstruct() {//        if (redisServer.isActive()) {////            redisServer.stop();//            Runtime.getRuntime().addShutdownHook(new Thread() {//                public void run() { redisServer.stop(); }//            });//        }        redisServer.start();    }    @PreDestroy    public void preDestroy() {//        redisServer.stop();        Runtime.getRuntime().addShutdownHook(new Thread(() -> redisServer.stop()));        redisServer.stop();    }    @Test    void saveStoreToRedis() {        List<Store> stores = new ArrayList<>();        initializeStores(stores);        when(repo.findAll()).thenReturn(stores);        List<StoreResponse> actualStoresResponse = storeDao.findAll();        Assert.assertNotNull(actualStoresResponse);//        cacheService.invalidateCache();    }}

I am getting error

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'xxx.CacheServiceImplTest.ORIGINAL': Invocation of init method failed; nested exception is java.lang.RuntimeException: Can't start redis server. Check logs for details.

But when I kill it manually things work just fine.

lsof -i:6370COMMAND     PID     USER   FD   TYPE             DEVICE SIZE/OFF NODE NAMEredis-ser 80714 XXX    4u  IPv4 0x5c18b66bd501f58d      0t0  TCP localhost:6370 (LISTEN) kill -9 80714

HttpServletRequest Serialization Issue with Redis

$
0
0

The application that I am working on uses Redis and as fas as I know Redis tries to serialize every objects that are being stored in session.I have a below line in my code which is troubling me:-

httpServletRequest.getSession.setAttribute(some_key, someObjectFactory.someMethod(httpServletRequest);

I am getting below exception which is being thrown from the above line of code.

ava.io.IOException: java.lang.RuntimeException: Class org.springframework.security.web.servletapi.HttpServlet3RequestFactory$Servlet3SecurityContextHolderAwareRequestWrapper does not implement Serializable or externalizable

The "SomeObjectFactory" class looks like below, after I made it to implement Serializable

@Componendpublic class SomeObjectFactory implements Serializable {//ssvuid declarationprivate Object1 object1;private Object2 object2;@Autowiredpublic SomeObjectFactory(Object1 object1, Object2 object2) {   this.object1 = object1;   this.object2 = object2;}public SomeVOObject someMethod(HttpServletRequest request){   return new SomeVOObject(request , new SomeObjectMapper(object1, object2)}

I have made all the dependent objects implements Serializable and everything but still I am getting "does not implement Serializable or externalizable" exception. I doubt this is because the "SomeVOObject" has httpServletRequest also as it's first argument in the constructor and httpServletRequest cann't be Serialized.

Please advise if there is any solution already available or any approach to overcome this.

Unable to read Redis Cache Names and Cache Keys

$
0
0

I have a Spring Boot service that use Redis as a cache store using @Cachable. The same service also contain the Eviction calls using CacheManager. I want to move the Evict feature to a difference service, which:- Reads all the Caches from the Redis- Evict All or By CacheName/CacheKey.

I setup the service but unable to query the cacheNames. CacheManager won't able to get all the caches from Redis Store. Please help me or share a complete solution. Thanks

Controller Unit Test How do I mock RedisTemplate opsForValue with return type ValueOperations

$
0
0

In my controller I have code like the following.RedisTemplate stringRedisTemplate

def accessRedis(){   val = stringRedisTemplate.opsForValue().get('Key')}

In my controller Test, I intent to inject a mocked RedisTemplate that return a mocked ValueOperations. My code:

def template = mockFor(RedisTemplate)       def val = mockFor(org.springframework.data.redis.core.ValueOperations)val.demand.get {p-> println "$p"}template.demand.opsForValue {    return val.createMock()}controller.stringRedisTemplate = template.createMock()controller.accessRedis()

However, I got the following error:org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'com.tnd.viewport.ui.AppHawkControllerSpec$_$spock_feature_0_1_closure2@1aa55dd5' with class 'com.tnd.viewport.ui.AppHawkControllerSpec$_$spock_feature_0_1_closure2' to class 'org.springframework.data.redis.core.ValueOperations'

Could you advice a solution for my scenario? Thanks!

Redisson client exception Netty threads

$
0
0

I have my application deployed in openshift and also it uses Redis. White it works most of the time , I still face issue related to redisson which is intermittent. The error trace is as below while launching the url of application :-

org.redisson.client.WriteRedisConnectionException: Unable to send command! Node source: NodeSource [slot=null, addr=null, redisClient=null, redirect=null, entry=MasterSlaveEntry [masterEntry=[freeSubscribeConnectionsAmount=0, freeSubscribeConnectionsCounter=value:49:queue:0, freeConnectionsAmount=31, freeConnectionsCounter=value:63:queue:0, freezed=false, freezeReason=null, client=[addr=redis://webapp-sessionstore.9m6hkf.ng.0001.apse2.cache.amazonaws.com:6379], nodeType=MASTER, firstFail=0]]], connection: RedisConnection@1568202974 [redisClient=[addr=redis://webapp-sessionstore.9m6hkf.ng.0001.apse2.cache.amazonaws.com:6379], channel=[id: 0xceaf7022, L:/10.103.34.74:32826 ! R:webapp-sessionstore.9m6hkf.ng.0001.apse2.cache.amazonaws.com/10.112.17.104:6379], currentCommand=CommandData [promise=RedissonPromise [promise=ImmediateEventExecutor$ImmediatePromise@68b1bc80(failure: java.util.concurrent.CancellationException)], command=(HMSET), params=[redisson:tomcat_session:306A0C0325AD2189A7FDDB695D0755D2, PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), PooledUnsafeDirectByteBuf(freed), ...], codec=org.redisson.codec.CompositeCodec@25e7216]], command: (HMSET), params: [redisson:tomcat_session:77C4BB9FC4252BFC2C8411F3A4DBB6C9, PooledUnsafeDirectByteBuf(ridx: 0, widx: 24, cap: 256), PooledUnsafeDirectByteBuf(ridx: 0, widx: 10, cap: 256), PooledUnsafeDirectByteBuf(ridx: 0, widx: 24, cap: 256), PooledUnsafeDirectByteBuf(ridx: 0, widx: 10, cap: 256)] after 3 retry attempts    org.redisson.command.CommandAsyncService.checkWriteFuture(CommandAsyncService.java:872)    org.redisson.command.CommandAsyncService.access$000(CommandAsyncService.java:97)    org.redisson.command.CommandAsyncService$7.operationComplete(CommandAsyncService.java:791)    org.redisson.command.CommandAsyncService$7.operationComplete(CommandAsyncService.java:788)    io.netty.util.concurrent.DefaultPromise.notifyListener0(DefaultPromise.java:502)    io.netty.util.concurrent.DefaultPromise.notifyListenersNow(DefaultPromise.java:476)    io.netty.util.concurrent.DefaultPromise.notifyListeners(DefaultPromise.java:415)    io.netty.util.concurrent.DefaultPromise.setValue0(DefaultPromise.java:540)    io.netty.util.concurrent.DefaultPromise.setFailure0(DefaultPromise.java:533)    io.netty.util.concurrent.DefaultPromise.tryFailure(DefaultPromise.java:114)    io.netty.channel.AbstractChannel$AbstractUnsafe.safeSetFailure(AbstractChannel.java:1018)    io.netty.channel.AbstractChannel$AbstractUnsafe.write(AbstractChannel.java:874)    io.netty.channel.DefaultChannelPipeline$HeadContext.write(DefaultChannelPipeline.java:1365)    io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:716)    io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:708)    io.netty.channel.AbstractChannelHandlerContext.access$1700(AbstractChannelHandlerContext.java:56)    io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.write(AbstractChannelHandlerContext.java:1102)    io.netty.channel.AbstractChannelHandlerContext$WriteAndFlushTask.write(AbstractChannelHandlerContext.java:1149)    io.netty.channel.AbstractChannelHandlerContext$AbstractWriteTask.run(AbstractChannelHandlerContext.java:1073)    io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163)    io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:405)    io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:500)    io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:906)    io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)    io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)    java.lang.Thread.run(Thread.java:748)Root Causeio.netty.channel.ExtendedClosedChannelException    io.netty.channel.AbstractChannel$AbstractUnsafe.write(...)(Unknown Source)Note The full stack trace of the root cause is available in the server logs.

Spring Data Redis : Is there a way to determine which redis node is spring querying against?

$
0
0

Im pretty new to Redis and im trying to implement redis using spring data redis making redis as a storage database.We have a master and two slaves and we included readOnly configuration from slave(SLAVE_PREFFERED).

Now, I would like to know if there is a way to know which redis node is being used to query the data when my application is using the database.

Thanks.


Range Querying in Redis - Spring Data Redis

$
0
0

is there a way we can implement Range queries in Redis using Spring Data Redis?

Eg:

If my Pojo class has Date(which is not a primary key) and i require data that falls under a desired period of date, Is it possible with Spring Data Redis to construct a query for the same rather than querying each date individually?

Proper location for common models and utils, for multi-module maven project using spring-boot

$
0
0

There is a multi-model maven project, with dir layout as following:

  xxx-parent/      common/      common-web/      member-api/      admin-api/      market-service/      ...

As you can see, there is a parent pom, and multiple sub modules, the common or common-xxx modules are the commons parts, which would be dependencies of other non-common modules, for code reuse.


Questions

  • A. Where would you put the model classes, which are usually POJO, with private fields, and getter/setters.
    These models might be reused by multiple non-common sub modules, (e.g both market-service and admin-api).
  • B. For some api, those model could be reused in the returned json, but only need part of the fields, would you create a separate POJO (usually I call it entity) for that case? And, if so, you would probably put it in the specific sub module (e.g admin-api), not the common projects, right?
  • C. If this is a spring-boot based project, and it use Redis, and we wrote a redis util, which could be reused for most redis operations.
    The problem is if we put the redis util and its maven dependencies into common, then it will connect to redis on application start automatically, but some of the sub modules don't need redis.
    The solutions I am considering are:
    • Create a separate sub module common-redis for the util.
      But there would be other utils too (e.g common-jdbc), and there seems to be too much of common modules.
    • Disable sprint-boot's auto config for a specific feature (e.g redis) via @EnableAutoConfiguration, so that, it won't try to connect to redis on start.

Unable to connect to Jedis in AndroidStudio (Java)

$
0
0

My apllication is crushing when I press the button which is responsible for connecting to Redis database using Jedis client and changing TextView to value which stores jedis.ping() (It should return "Pong"). Code and Log are below. Same operations (connection to Redis and printing jedis.ping()) works perfectly in Eclipse. Maybe I insert code connected with Jedis in wrong place? I know too litle about Android programming. Thanks in advance!

package com.samfoundation.samfoundation;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import redis.clients.jedis.Jedis;public class MainActivity extends AppCompatActivity {    Button click;    TextView show;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        click = (Button)findViewById(R.id.button);        show = (TextView)findViewById(R.id.textView);        click.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Jedis jedis = new Jedis("localhost");                String value = jedis.ping();                show.setText(value);            }        });    }}

Log:

2020-04-09 19:53:22.473 32666-32666/com.samfoundation.samfoundation E/AndroidRuntime: FATAL EXCEPTION: main    Process: com.samfoundation.samfoundation, PID: 32666    redis.clients.jedis.exceptions.JedisConnectionException: Failed connecting to host localhost:6379        at redis.clients.jedis.Connection.connect(Connection.java:204)        at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)        at redis.clients.jedis.Connection.sendCommand(Connection.java:125)        at redis.clients.jedis.Connection.sendCommand(Connection.java:120)        at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:113)        at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:188)        at com.samfoundation.samfoundation.MainActivity$1.onClick(MainActivity.java:33)        at android.view.View.performClick(View.java:7259)        at android.view.View.performClickInternal(View.java:7236)        at android.view.View.access$3600(View.java:801)        at android.view.View$PerformClick.run(View.java:27892)        at android.os.Handler.handleCallback(Handler.java:883)        at android.os.Handler.dispatchMessage(Handler.java:100)        at android.os.Looper.loop(Looper.java:214)        at android.app.ActivityThread.main(ActivityThread.java:7356)        at java.lang.reflect.Method.invoke(Native Method)        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)     Caused by: java.net.SocketException: socket failed: EACCES (Permission denied)        at java.net.Socket.createImpl(Socket.java:492)        at java.net.Socket.getImpl(Socket.java:552)        at java.net.Socket.setReuseAddress(Socket.java:1493)        at redis.clients.jedis.Connection.connect(Connection.java:171)        at redis.clients.jedis.BinaryClient.connect(BinaryClient.java:100)         at redis.clients.jedis.Connection.sendCommand(Connection.java:125)         at redis.clients.jedis.Connection.sendCommand(Connection.java:120)         at redis.clients.jedis.BinaryClient.ping(BinaryClient.java:113)         at redis.clients.jedis.BinaryJedis.ping(BinaryJedis.java:188)         at com.samfoundation.samfoundation.MainActivity$1.onClick(MainActivity.java:33)         at android.view.View.performClick(View.java:7259)         at android.view.View.performClickInternal(View.java:7236)         at android.view.View.access$3600(View.java:801)         at android.view.View$PerformClick.run(View.java:27892)         at android.os.Handler.handleCallback(Handler.java:883)         at android.os.Handler.dispatchMessage(Handler.java:100)         at android.os.Looper.loop(Looper.java:214)         at android.app.ActivityThread.main(ActivityThread.java:7356)         at java.lang.reflect.Method.invoke(Native Method)         at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.samfoundation.samfoundation"><application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme"><activity android:name=".MainActivity"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>

How to use composite key as id in spring redis entity as equivalent to @embedded id

$
0
0

I am using a spring data redis repository for retrieval and persistence into Redis. But I need the id to be composite of 2 fields. How can this be achieved.Is there any way equivalent to @EmbeddedId?

Autocomplete with java , Redis, Elastic Search , Mongo

$
0
0

I have to implement an autocomplete with over 500,000 names which may later increase to over 4 million names.

Backend is a java REST web service call using Spring. Should I use MongoDB, Redis or Elasticsearch for storing and querying/searching the names?

Viewing all 2203 articles
Browse latest View live


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>