|
| 1 | +/* |
| 2 | + * Copyright 2022-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.mqtt.core; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | + |
| 23 | +import org.apache.commons.logging.Log; |
| 24 | +import org.apache.commons.logging.LogFactory; |
| 25 | + |
| 26 | +import org.springframework.context.ApplicationEventPublisher; |
| 27 | +import org.springframework.context.ApplicationEventPublisherAware; |
| 28 | +import org.springframework.context.SmartLifecycle; |
| 29 | +import org.springframework.integration.mqtt.inbound.AbstractMqttMessageDrivenChannelAdapter; |
| 30 | +import org.springframework.util.Assert; |
| 31 | + |
| 32 | +/** |
| 33 | + * Abstract class for MQTT client managers which can be a base for any common v3/v5 client manager implementation. |
| 34 | + * Contains some basic utility and implementation-agnostic fields and methods. |
| 35 | + * |
| 36 | + * @param <T> MQTT client type |
| 37 | + * @param <C> MQTT connection options type (v5 or v3) |
| 38 | + * |
| 39 | + * @author Artem Vozhdayenko |
| 40 | + * @author Artem Bilan |
| 41 | + * |
| 42 | + * @since 6.0 |
| 43 | + */ |
| 44 | +public abstract class AbstractMqttClientManager<T, C> implements ClientManager<T, C>, ApplicationEventPublisherAware { |
| 45 | + |
| 46 | + protected final Log logger = LogFactory.getLog(this.getClass()); // NOSONAR |
| 47 | + |
| 48 | + private static final int DEFAULT_MANAGER_PHASE = 0; |
| 49 | + |
| 50 | + private final Set<ConnectCallback> connectCallbacks = Collections.synchronizedSet(new HashSet<>()); |
| 51 | + |
| 52 | + private final String clientId; |
| 53 | + |
| 54 | + private int phase = DEFAULT_MANAGER_PHASE; |
| 55 | + |
| 56 | + private boolean manualAcks; |
| 57 | + |
| 58 | + private ApplicationEventPublisher applicationEventPublisher; |
| 59 | + |
| 60 | + private String url; |
| 61 | + |
| 62 | + private String beanName; |
| 63 | + |
| 64 | + private volatile T client; |
| 65 | + |
| 66 | + protected AbstractMqttClientManager(String clientId) { |
| 67 | + Assert.notNull(clientId, "'clientId' is required"); |
| 68 | + this.clientId = clientId; |
| 69 | + } |
| 70 | + |
| 71 | + public void setManualAcks(boolean manualAcks) { |
| 72 | + this.manualAcks = manualAcks; |
| 73 | + } |
| 74 | + |
| 75 | + protected String getUrl() { |
| 76 | + return this.url; |
| 77 | + } |
| 78 | + |
| 79 | + protected void setUrl(String url) { |
| 80 | + this.url = url; |
| 81 | + } |
| 82 | + |
| 83 | + protected String getClientId() { |
| 84 | + return this.clientId; |
| 85 | + } |
| 86 | + |
| 87 | + protected ApplicationEventPublisher getApplicationEventPublisher() { |
| 88 | + return this.applicationEventPublisher; |
| 89 | + } |
| 90 | + |
| 91 | + protected synchronized void setClient(T client) { |
| 92 | + this.client = client; |
| 93 | + } |
| 94 | + |
| 95 | + protected Set<ConnectCallback> getCallbacks() { |
| 96 | + return this.connectCallbacks; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public boolean isManualAcks() { |
| 101 | + return this.manualAcks; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public T getClient() { |
| 106 | + return this.client; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { |
| 111 | + Assert.notNull(applicationEventPublisher, "'applicationEventPublisher' cannot be null"); |
| 112 | + this.applicationEventPublisher = applicationEventPublisher; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void setBeanName(String name) { |
| 117 | + this.beanName = name; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public String getBeanName() { |
| 122 | + return this.beanName; |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * The phase of component autostart in {@link SmartLifecycle}. |
| 127 | + * If the custom one is required, note that for the correct behavior it should be less than phase of |
| 128 | + * {@link AbstractMqttMessageDrivenChannelAdapter} implementations. |
| 129 | + * The default phase is {@link #DEFAULT_MANAGER_PHASE}. |
| 130 | + * @return {@link SmartLifecycle} autostart phase |
| 131 | + * @see #setPhase |
| 132 | + */ |
| 133 | + @Override |
| 134 | + public int getPhase() { |
| 135 | + return this.phase; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void addCallback(ConnectCallback connectCallback) { |
| 140 | + this.connectCallbacks.add(connectCallback); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public boolean removeCallback(ConnectCallback connectCallback) { |
| 145 | + return this.connectCallbacks.remove(connectCallback); |
| 146 | + } |
| 147 | + |
| 148 | + public synchronized boolean isRunning() { |
| 149 | + return this.client != null; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Set the phase of component autostart in {@link SmartLifecycle}. |
| 154 | + * If the custom one is required, note that for the correct behavior it should be less than phase of |
| 155 | + * {@link AbstractMqttMessageDrivenChannelAdapter} implementations. |
| 156 | + * @see #getPhase |
| 157 | + */ |
| 158 | + public void setPhase(int phase) { |
| 159 | + this.phase = phase; |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments