現在、スプリング ブートを使用してマイクロサービスを開発しています。現在、スプリング ステート マシンとスプリング クラウド スルース アーティファクトを一緒に使用するときに問題があります。
@Validated
@RestController
@SuppressWarnings({"squid:S00112"})
@RequestMapping()
public class StatusController {
@Autowired
private QuoteService quoteService;
@Autowired
private StateMachine<StateMachineDefinition.States, StateMachineDefinition.Events> stateMachine;
@Autowired
private QuoteStateHandler quoteStateHandler;
@Value("${StateMachine.InvalidField.message}")
private String statusInvalidField;
@Value("${StateMachine.QuoteCannotBeNull.message}")
private String quoteStatusNull;
private static final String STATUS = "STATUS";
@InitBinder
public void initBinder(WebDataBinder binder, WebRequest request) {
binder.setAllowedFields("status");
}
/*
Possible state transitions for the specific quote.
*/
@RequestMapping(method = RequestMethod.GET, value = {"/quotes/{quoteId}/transitions", "/{internalClient:(?:ui|s2s)}/{version:^[v]+[0-9]+$}/quotes/{quoteId}/transitions"})
public List<Status> getPossibleTransitions(@PathVariable("quoteId") String id) {
String persistedStatus = quoteService.findOne(id).getStatus();
if (persistedStatus == null) {
persistedStatus = StateMachineDefinition.States.IN_PROCESS.name();
}
Collection<Transition<StateMachineDefinition.States, StateMachineDefinition.Events>> transitions = stateMachine.getTransitions();
String currentState = persistedStatus;
ArrayList<Status> possibleTransistions = new ArrayList<>();
Iterator<Transition<StateMachineDefinition.States, StateMachineDefinition.Events>> iterator = transitions.iterator();
String state;
while (iterator.hasNext()) {
Transition<StateMachineDefinition.States, StateMachineDefinition.Events> transition = iterator.next();
state = transition.getSource().getId().name();
if (state.compareTo(currentState) == 0) {
possibleTransistions.add(new Status(transition.getTrigger().getEvent().toString()));
}
}
return possibleTransistions;
}
@RequestMapping(method = RequestMethod.GET, value = {"/{internalClient:(?:ui)}/{version:^[v]+[0-9]+$}/states"})
public List<String> getStates() {
Collection<State<StateMachineDefinition.States, StateMachineDefinition.Events>> states = stateMachine.getStates();
Iterator<State<StateMachineDefinition.States, StateMachineDefinition.Events>> iterator = states.iterator();
List<String> stateList = new ArrayList<>();
while (iterator.hasNext()) {
State<StateMachineDefinition.States, StateMachineDefinition.Events> state = iterator.next();
stateList.add(state.getId().toString());
}
return stateList;
}
/*
Status is not a state but a transition or event.
*/
@RequestMapping(method = RequestMethod.POST, value = {"{quoteId}/transitions", "/{internalClient:(?:ui|s2s)}/{version:^[v]+[0-9]+$}/quotes/{quoteId}/transitions"})
@ResponseStatus(HttpStatus.NO_CONTENT)
public void postStatus(@RequestBody @Validated(Groups.Api.class) Status status, @PathVariable("quoteId") @Pattern(regexp = Patterns.UUID) String id, BindingResult bindingResult) throws Exception {
if (bindingResult.hasErrors()) {
throw new BadRequestValidationException(STATUS, statusInvalidField);
}
//get quoteid current status
Quote currentQuote = quoteService.findOne(id);
if (currentQuote.getStatus() != null) {
StateMachineDefinition.States currentQuoteState = StateMachineDefinition.States.valueOf(currentQuote.getStatus());
//need to send the event and let the state listener evaluate.
if (status.getStatus() != null) {
quoteStateHandler.handleEvent(
MessageBuilder
.withPayload(StateMachineDefinition.Events.valueOf(status.getStatus()))
.setHeader("quote-id", id)
.build(), currentQuoteState);
}
if (stateMachine.getExtendedState().getVariables().containsKey("ERROR")) {
Exception exception = (Exception) stateMachine.getExtendedState().getVariables().get("ERROR");
stateMachine.getExtendedState().getVariables().clear();
throw exception;
}
if (stateMachine.getState().getId() != currentQuoteState) {
quoteService.updateStatus(id, stateMachine.getState().getId());
}
} else {
//If a quote has null status then it wasnt created properly.
throw new BadRequestValidationException(STATUS, quoteStatusNull);
}
}
}
spring-cloud sleuth の依存関係を追加し、「mvn clean install」を開始したときにエラーがポップアップするまで、問題はありませんでした。
エラー スタック トレース:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stateMachine': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'stateMachineTaskExecutor' is expected to be of type [org.springframework.core.task.TaskExecutor] but was actually of type [org.springframework.cloud.sleuth.instrument.async.LazyTraceExecutor]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:207)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1128)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1056)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:566)
... 44 common frames omitted
エラーメッセージ org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stateMachine': Invocation of init method failed; ネストされた例外は org.springframework.beans.factory.BeanNotOfRequiredTypeException: 'stateMachineTaskExecutor' という名前の Bean はタイプ [org.springframework.core.task.TaskExecutor] であると予想されますが、実際にはタイプ [org.springframework.cloud.sleuth.instrument] でした.async.LazyTraceExecutor]
2 つの依存関係を持つ pom.xml ファイルを次に示します。
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Camden.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth</artifactId>
<version>1.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependency>
<groupId>org.springframework.statemachine</groupId>
<artifactId>spring-statemachine-core</artifactId>
<version>LATEST</version>
<dependency>
春のアプリケーションコンテキストに、具体的にどのタイプをロードする必要があるかをどのように知らせる必要がありますか?両方のクラスが Java util パッケージの同じエグゼキューターを使用しているためです。
java.util.concurrent.Executor