冲突的枚举值 java
Conflicting enum values java
我在 2 个单独的文件中定义了 2 个枚举,如下所示:
MyErrorCodes.java
@Getter
public enum MyErrorCodes implements ErrorCode {
ERROR1(90, 1, 01),
ERROR2(90, 1, 02),
ERROR3(90, 1, 03),
ERROR4(90, 1, 04),
ERROR5(90, 1, 05);
....
}
ErrorCategory.java
public class ErrorCategory {
@AllArgsConstructor
public enum ErrorCodes {
EXECUTION_ERROR("execution.error", "Error in executing {0}.", INTERNAL_ERROR,
MyErrorCodes.ERROR1),
DESERIALIZATION_ERROR("...", "...",BAD_REQUEST_ERROR, MyErrorCodes.ERROR2),
DEFAULT_INTERNAL_ERROR("...", "...", INTERNAL_ERROR, MyErrorCodes.ERROR3),
INVALID_RESPONSE("...", "...", INTERNAL_ERROR, MyErrorCodes.ERROR4),
MAPPING_ERROR("...", "...", INTERNAL_ERROR, MyErrorCodes.ERROR5);
}
}
在运行时出现错误:
Conflicting enum values. Name 'ERROR4' uses ordinal value (4) that is also used for name 'MAPPING_ERROR'
ERROR4
是枚举#1 中的第 4 个元素,而 MAPPING_ERROR
是枚举#2 中的第 5 个元素。
那为什么当它们是完全不同的枚举时我会收到冲突错误?
P.S。 enum#1 实现的接口如下所示:
public interface ErrorCode {
int getErrorCode();
String getKey();
}
堆栈跟踪:
at io.opentracing.contrib.concurrent.TracedRunnable.run(TracedRunnable.java:30)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.apache.ignite.binary.BinaryObjectException: Conflicting enum values. Name 'ERROR4' uses ordinal value (4) that is also used for name 'MAPPING_ERROR'
at org.apache.ignite.internal.binary.BinaryUtils.mergeEnumValues(BinaryUtils.java:2538)
at org.apache.ignite.internal.binary.BinaryUtils.mergeMetadata(BinaryUtils.java:1028)
at org.apache.ignite.internal.processors.cache.binary.BinaryMetadataTransport.requestMetadataUpdate(BinaryMetadataTransport.java:211)
at org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl.addMeta(CacheObjectBinaryProcessorImpl.java:606)
at org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl.addMeta(CacheObjectBinaryProcessorImpl.java:288)
at org.apache.ignite.internal.binary.BinaryContext.registerUserClassDescriptor(BinaryContext.java:828)
at org.apache.ignite.internal.binary.BinaryContext.registerDescriptor(BinaryContext.java:784)
at org.apache.ignite.internal.binary.BinaryContext.registerClass(BinaryContext.java:581)
at org.apache.ignite.internal.binary.BinaryContext.registerClass(BinaryContext.java:556)
at org.apache.ignite.internal.binary.BinaryWriterExImpl.doWriteEnum(BinaryWriterExImpl.java:829)
at org.apache.ignite.internal.binary.BinaryWriterExImpl.writeEnumField(BinaryWriterExImpl.java:1323)
at org.apache.ignite.internal.binary.BinaryFieldAccessor$DefaultFinalClassAccessor.write0(BinaryFieldAccessor.java:670)
at org.apache.ignite.internal.binary.BinaryFieldAccessor.write(BinaryFieldAccessor.java:157)
... 115 common frames omitted
```
好的,下面是我们针对该问题所做的工作和发现:
我们使用 ignite cluster 进行部署,我们将 ignite 版本从 6 更新到 7 并解决了问题。
Ignite 缓存枚举值,所以当我们引入新的枚举值时,我们应该添加到末尾以增加序号以避免将来发生冲突或清除缓存。
我在 2 个单独的文件中定义了 2 个枚举,如下所示:
MyErrorCodes.java
@Getter
public enum MyErrorCodes implements ErrorCode {
ERROR1(90, 1, 01),
ERROR2(90, 1, 02),
ERROR3(90, 1, 03),
ERROR4(90, 1, 04),
ERROR5(90, 1, 05);
....
}
ErrorCategory.java
public class ErrorCategory {
@AllArgsConstructor
public enum ErrorCodes {
EXECUTION_ERROR("execution.error", "Error in executing {0}.", INTERNAL_ERROR,
MyErrorCodes.ERROR1),
DESERIALIZATION_ERROR("...", "...",BAD_REQUEST_ERROR, MyErrorCodes.ERROR2),
DEFAULT_INTERNAL_ERROR("...", "...", INTERNAL_ERROR, MyErrorCodes.ERROR3),
INVALID_RESPONSE("...", "...", INTERNAL_ERROR, MyErrorCodes.ERROR4),
MAPPING_ERROR("...", "...", INTERNAL_ERROR, MyErrorCodes.ERROR5);
}
}
在运行时出现错误:
Conflicting enum values. Name 'ERROR4' uses ordinal value (4) that is also used for name 'MAPPING_ERROR'
ERROR4
是枚举#1 中的第 4 个元素,而 MAPPING_ERROR
是枚举#2 中的第 5 个元素。
那为什么当它们是完全不同的枚举时我会收到冲突错误?
P.S。 enum#1 实现的接口如下所示:
public interface ErrorCode {
int getErrorCode();
String getKey();
}
堆栈跟踪:
at io.opentracing.contrib.concurrent.TracedRunnable.run(TracedRunnable.java:30)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: org.apache.ignite.binary.BinaryObjectException: Conflicting enum values. Name 'ERROR4' uses ordinal value (4) that is also used for name 'MAPPING_ERROR'
at org.apache.ignite.internal.binary.BinaryUtils.mergeEnumValues(BinaryUtils.java:2538)
at org.apache.ignite.internal.binary.BinaryUtils.mergeMetadata(BinaryUtils.java:1028)
at org.apache.ignite.internal.processors.cache.binary.BinaryMetadataTransport.requestMetadataUpdate(BinaryMetadataTransport.java:211)
at org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl.addMeta(CacheObjectBinaryProcessorImpl.java:606)
at org.apache.ignite.internal.processors.cache.binary.CacheObjectBinaryProcessorImpl.addMeta(CacheObjectBinaryProcessorImpl.java:288)
at org.apache.ignite.internal.binary.BinaryContext.registerUserClassDescriptor(BinaryContext.java:828)
at org.apache.ignite.internal.binary.BinaryContext.registerDescriptor(BinaryContext.java:784)
at org.apache.ignite.internal.binary.BinaryContext.registerClass(BinaryContext.java:581)
at org.apache.ignite.internal.binary.BinaryContext.registerClass(BinaryContext.java:556)
at org.apache.ignite.internal.binary.BinaryWriterExImpl.doWriteEnum(BinaryWriterExImpl.java:829)
at org.apache.ignite.internal.binary.BinaryWriterExImpl.writeEnumField(BinaryWriterExImpl.java:1323)
at org.apache.ignite.internal.binary.BinaryFieldAccessor$DefaultFinalClassAccessor.write0(BinaryFieldAccessor.java:670)
at org.apache.ignite.internal.binary.BinaryFieldAccessor.write(BinaryFieldAccessor.java:157)
... 115 common frames omitted
```
好的,下面是我们针对该问题所做的工作和发现:
我们使用 ignite cluster 进行部署,我们将 ignite 版本从 6 更新到 7 并解决了问题。
Ignite 缓存枚举值,所以当我们引入新的枚举值时,我们应该添加到末尾以增加序号以避免将来发生冲突或清除缓存。