Improve Spring Bus usage (remove stream direct use) (#2521)
* Improve Spring Bus usage (remove stream direct use) Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com> * Remove getApplicaton when creating remote events --------- Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
@@ -14,8 +14,8 @@ import java.util.concurrent.Executor;
|
||||
import io.protostuff.ProtostuffIOUtil;
|
||||
import io.protostuff.Schema;
|
||||
import org.eclipse.hawkbit.repository.event.ApplicationEventFilter;
|
||||
import org.eclipse.hawkbit.repository.event.EventPublisherHolder;
|
||||
import org.eclipse.hawkbit.repository.event.remote.RemoteTenantAwareEvent;
|
||||
import org.eclipse.hawkbit.repository.model.helper.EventPublisherHolder;
|
||||
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
@@ -61,8 +61,7 @@ public class EventPublisherConfiguration {
|
||||
}
|
||||
|
||||
/**
|
||||
* Bean for creating a singleton instance of the
|
||||
* {@link EventPublisherHolder}
|
||||
* Bean for creating a singleton instance of the {@link EventPublisherHolder}
|
||||
*
|
||||
* @return the singleton instance of the {@link EventPublisherHolder}
|
||||
*/
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
/**
|
||||
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
|
||||
*
|
||||
* This program and the accompanying materials are made
|
||||
* available under the terms of the Eclipse Public License 2.0
|
||||
* which is available at https://www.eclipse.org/legal/epl-2.0/
|
||||
*
|
||||
* SPDX-License-Identifier: EPL-2.0
|
||||
*/
|
||||
package org.eclipse.hawkbit.repository.model.helper;
|
||||
|
||||
import lombok.AccessLevel;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.bus.BusProperties;
|
||||
import org.springframework.cloud.bus.ServiceMatcher;
|
||||
import org.springframework.context.ApplicationEventPublisher;
|
||||
|
||||
/**
|
||||
* A singleton bean which holds the event publisher and service origin id in order to publish remote application events.
|
||||
* It can be used in beans not instantiated by spring e.g. JPA entities which cannot be auto-wired.
|
||||
*/
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
@SuppressWarnings("java:S6548") // java:S6548 - singleton holder ensures static access to spring resources in some places
|
||||
public final class EventPublisherHolder {
|
||||
|
||||
private static final EventPublisherHolder SINGLETON = new EventPublisherHolder();
|
||||
|
||||
@Getter
|
||||
private ApplicationEventPublisher eventPublisher;
|
||||
private ServiceMatcher serviceMatcher;
|
||||
private BusProperties bus;
|
||||
|
||||
/**
|
||||
* @return the event publisher holder singleton instance
|
||||
*/
|
||||
public static EventPublisherHolder getInstance() {
|
||||
return SINGLETON;
|
||||
}
|
||||
|
||||
@Autowired // spring setter injection
|
||||
public void setApplicationEventPublisher(final ApplicationEventPublisher eventPublisher) {
|
||||
this.eventPublisher = eventPublisher;
|
||||
}
|
||||
|
||||
@Autowired(required = false) // spring setter injection
|
||||
public void setServiceMatcher(final ServiceMatcher serviceMatcher) {
|
||||
this.serviceMatcher = serviceMatcher;
|
||||
}
|
||||
|
||||
@Autowired // spring setter injection
|
||||
public void setBusProperties(final BusProperties bus) {
|
||||
this.bus = bus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the service origin Id coming either from {@link ServiceMatcher} when available or {@link BusProperties}
|
||||
* otherwise.
|
||||
*/
|
||||
public String getApplicationId() {
|
||||
String id = null;
|
||||
if (serviceMatcher != null) {
|
||||
id = serviceMatcher.getBusId();
|
||||
}
|
||||
if (id == null) {
|
||||
id = bus.getId();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
}
|
||||
@@ -13,6 +13,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.Serial;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.eclipse.hawkbit.repository.event.remote.entity.RemoteEntityEvent;
|
||||
@@ -49,7 +50,7 @@ class BusProtoStuffMessageConverterTest {
|
||||
*/
|
||||
@Test
|
||||
void successfullySerializeAndDeserializeEvent() {
|
||||
final TargetCreatedEvent targetCreatedEvent = new TargetCreatedEvent(targetMock, "1");
|
||||
final TargetCreatedEvent targetCreatedEvent = new TargetCreatedEvent(targetMock);
|
||||
// serialize
|
||||
final Object serializedEvent = underTest.convertToInternal(targetCreatedEvent,
|
||||
new MessageHeaders(new HashMap<>()), null);
|
||||
@@ -68,7 +69,7 @@ class BusProtoStuffMessageConverterTest {
|
||||
*/
|
||||
@Test
|
||||
void missingEventTypeMappingThrowsMessageConversationException() {
|
||||
final DummyRemoteEntityEvent dummyEvent = new DummyRemoteEntityEvent(targetMock, "applicationId");
|
||||
final DummyRemoteEntityEvent dummyEvent = new DummyRemoteEntityEvent(targetMock);
|
||||
final MessageHeaders messageHeaders = new MessageHeaders(new HashMap<>());
|
||||
|
||||
assertThatExceptionOfType(MessageConversionException.class)
|
||||
@@ -79,13 +80,13 @@ class BusProtoStuffMessageConverterTest {
|
||||
/**
|
||||
* Test event with which non-existing mapping to serialize.
|
||||
*/
|
||||
private final class DummyRemoteEntityEvent extends RemoteEntityEvent<Target> {
|
||||
private static final class DummyRemoteEntityEvent extends RemoteEntityEvent<Target> {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private DummyRemoteEntityEvent(final Target target, final String applicationId) {
|
||||
super(target, applicationId);
|
||||
private DummyRemoteEntityEvent(final Target target) {
|
||||
super(target);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user