Embedded redis cant start redis server

This article is still about the embedded dependencies needed for integration tests. Yes, as the title said, embedded redis.

We use ozimov embedded redis which is the development of kstyrc embedded redis.

Let’s begin by adding ozimov embedded redis dependency.

<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>0.7.3</version>
</dependency>

Followed by adding the embedded server property to application.properties (src/test/resources)

spring.redis.host=localhost
spring.redis.port=6370

Now, let’s say we have an API to get student data from redis. Here is a sample code of it:

//StudentController.java
...
@GetMapping(value = {"/student-from-redis"}, produces = {MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public Student findByNameFromRedis(@RequestParam String name) {
return studentService.findByNameFromRedis(name);
}
...
//StudentService.java
...
Student findByNameFromRedis(String name);
...
//StudentServiceImpl.java
...
@Override
public Student findByNameFromRedis(String name) {
try {
String studentInString = redisTemplate.opsForValue().get("Student-" + name);
return objectMapper.readValue(studentInString, Student.class);
} catch (Exception e) {
return null;
}
}
...

At he StudentIntegrationTest.java, we need to autowire RedisTemplate to write the data into redis and ObjectMapper just to convert object to String. And then we create RedisServer object

...
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.data.redis.core.RedisTemplate;
import redis.embedded.RedisServer;
...

@Autowired
private MockMvc mockMvc;

@Autowired
private RedisTemplate<String, String> redisTemplate;
private RedisServer redisServer;

Then we initialize the redisServer under setUp method

@Before
public void setUp() {
try {
redisServer = RedisServer.builder().port(6370).build();
redisServer.start();
} catch (Exception e) {
//do nothing
}
}

And don’t forget to stop what we have started. :D

@After
public void tearDown() {
redisServer.stop();
}

On Windows OS, when you run the integration test, you might get the following error:

Caused by: java.lang.RuntimeException: Can't start redis server. Check logs for details.
at redis.embedded.AbstractRedisInstance.awaitRedisServerReady(AbstractRedisInstance.java:61)
at redis.embedded.AbstractRedisInstance.start(AbstractRedisInstance.java:39)
at redis.embedded.RedisServer.start(RedisServer.java:9)

And if you’re diligent enough to debug on AbstractRedisInstance.java:61, you’ll get this message:

The Windows version of Redis allocates a memory mapped heap for sharing with the forked process used for persistence operations. In order to share this memory, Windows allocates from the system paging file a portion equal to the size of the Redis heap

To fix that, you can add setting max memory on redisServer initialization

redisServer = RedisServer.builder().port(6370).setting("maxmemory 128M").build();

Finally, we can start the test method. First, we initialize the student object which we will store in redis.

@Test
public void findByNameFromRedisTest() throws Exception {
Student student = new Student();
student.setId(UUID.randomUUID().toString());
student.setName("George");
student.setAddress("Lavender Street");

convert the student object into String to store it to redis.

try {
String studentInString = objectMapper.writeValueAsString(student);
this.redisTemplate.opsForValue()
.set("Student-George", studentInString, 300, TimeUnit.SECONDS);
} catch (Exception e) {
// do nothing
}

Last we call the API and assert the result.

  this.mockMvc
.perform(get("/student-from-redis")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.param("name", "George")
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.name").value("George"))
.andExpect(jsonPath("$.address").value("Lavender Street"))
.andReturn();
}

That’s it. Quite simple, right? :D
Have a good day friends…

java.lang.RuntimeException: Cant start redis server. Check logs for details

Questions : java.lang.RuntimeException: Cant start redis server. Check logs for details

2022-09-28T05:07:45+00:00 2022-09-28T05:07:45+00:00

810

I am using Redis server of Windows version anycodings_windows 3.2.x (MSOpenTech Redis 3.2 Release Notes). anycodings_windows I am developing Spring Boot + Spring Data anycodings_windows Redis example and I am contineously getting anycodings_windows the below error.

java.lang.RuntimeException: Can't start redis server. Check logs for details.
    at redis.embedded.AbstractRedisInstance.awaitRedisServerReady(AbstractRedisInstance.java:61)
    at redis.embedded.AbstractRedisInstance.start(AbstractRedisInstance.java:39)
    at redis.embedded.RedisServer.start(RedisServer.java:9)
    at com.example.test.config.EmbeddedRedisServer.before(EmbeddedRedisServer.java:38)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

In this case I always need to restart my anycodings_windows machine. Is there any solution? I dont have anycodings_windows admin rights on my machine, so machine anycodings_windows reboot is the only option.

Total Answers 2

28

Answers 1 : of java.lang.RuntimeException: Cant start redis server. Check logs for details

Stop any Redis services operating anycodings_windows currently outside the IDE or change the anycodings_windows embedded Redis port to a different port

0

2022-09-28T05:07:45+00:00 2022-09-28T05:07:45+00:00Answer Link

mRahman

3

Answers 2 : of java.lang.RuntimeException: Cant start redis server. Check logs for details

For me change of port didn't work out. anycodings_windows so I opened task manager and then under anycodings_windows details tab killed anycodings_windows redis-server-0.7.2.exe process and it anycodings_windows worked.

0

2022-09-28T05:07:45+00:00 2022-09-28T05:07:45+00:00Answer Link

jidam