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

#2272 Do not fail in startup time if LockProvider is not found #2274

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.lang.reflect.Method;
import net.javacrumbs.shedlock.core.LockProvider;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

/**
* Not public now. If you think you need your LockProviderSupplier please create an issue and explain your use-case.
Expand All @@ -15,10 +14,10 @@ interface LockProviderSupplier {
static LockProviderSupplier create(ListableBeanFactory beanFactory) {
// Only fetching beanNames as the beans might not have been initialized yet.
String[] beanNamesForType = beanFactory.getBeanNamesForType(LockProvider.class);
if (beanNamesForType.length == 0) {
throw new NoSuchBeanDefinitionException(LockProvider.class, "No LockProvider bean found.");
}
if (beanNamesForType.length == 1) {
// If there are no beans of LockProvider type, we can't fail here as in older version we
// did not fail, and it's quire common in tests. To maintain backward compatibility
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// did not fail, and it's quire common in tests. To maintain backward compatibility
// did not fail, and it's quite common in tests. To maintain backward compatibility

// the failure will happen in runtime.
if (beanNamesForType.length <= 1) {
return (target, method, arguments) -> beanFactory.getBean(LockProvider.class);
}
return new BeanNameSelectingLockProviderSupplier(beanFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@
*/
package net.javacrumbs.shedlock.test.boot;

import javax.sql.DataSource;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import net.javacrumbs.shedlock.spring.annotation.EnableSchedulerLock;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
Expand All @@ -30,9 +26,4 @@ public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}

@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(dataSource, "shedlock");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.javacrumbs.shedlock.test.boot;

import javax.sql.DataSource;
import net.javacrumbs.shedlock.core.LockProvider;
import net.javacrumbs.shedlock.provider.jdbctemplate.JdbcTemplateLockProvider;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ShedlockConfig {
@Bean
public LockProvider lockProvider(DataSource dataSource) {
return new JdbcTemplateLockProvider(dataSource, "shedlock");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.javacrumbs.shedlock.test.boot;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(HelloController.class)
class HelloControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
void shouldCallController() throws Exception {
mockMvc.perform(get("/")).andExpect(status().isOk());
}
}