Reputation: 11
I have hornetQ application working. I am trying to move it to artemis.
I use jmsTemplate to exchange messages. the jmsTemplate.sendAndReceive() works for both server and client. I create a consumer with custom messageListener for both server and client. It works on hornetQ, but the messages sent to them never arrive on the messageListener.
do I have to change something in the session.createConsumer(tq).setMessageListener(new MyMessageListener) ?
i got this on server log:
org.apache.activemq.artemis.core.server : AMQ221003: Deploying queue jms.queue.e746ebf4-de2d-4257-84b0-975d94b5536a
org.apache.activemq.artemis.core.server : AMQ222165: No Dead Letter Address configured for queue jms.queue.e746ebf4-de2d-4257-84b0-975d94b5536a in AddressSettings
org.apache.activemq.artemis.core.server : AMQ222166: No Expiry Address configured for queue jms.queue.e746ebf4-de2d-4257-84b0-975d94b5536a in AddressSettings
After 1-2 minutes it loses connection and request for a new one.
application.properties
spring.artemis.mode=embedded
spring.artemis.embedded.enabled=true
spring.artemis.embedded.queues=connection
Customizer
@Component
public class ArtemisCustomizer {
@Bean
public ArtemisConfigurationCustomizer artemisConfigurationCustomizer() {
return new ArtemisConfigurationCustomizer() {
@Override
public void customize(Configuration configuration) {
Map<String, Object> transportProperties = new HashMap<String, Object>();
transportProperties.put(TransportConstants.HOST_PROP_NAME, "0.0.0.0");
transportProperties.put(TransportConstants.PORT_PROP_NAME, port);
// transportProperties.put(TransportConstants.SSL_ENABLED_PROP_NAME, true);
// transportProperties.put(TransportConstants.KEYSTORE_PATH_PROP_NAME, keystorePath);
// transportProperties.put(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME, keystorePassword);
// transportProperties.put(TransportConstants.TRUSTSTORE_PATH_PROP_NAME, truststorePath);
// transportProperties.put(TransportConstants.TRUSTSTORE_PASSWORD_PROP_NAME, truststorePassword);
Set<TransportConfiguration> acceptors = configuration.getAcceptorConfigurations();
acceptors.add(new TransportConfiguration(NettyAcceptorFactory.class.getName(), transportProperties));
}
};
}
}
Consumer
@Component
public class ConnectionConsumer {
private Map<Long, String> queueMap = new HashMap<>();
@JmsListener(destination = "connection")
public void process(Message message) {
log.info("JMS message received: {}", message);
try {
Client client = agentConnected(message.getBody(AgentConnection.class)); // here it logs the client connected and get the info
if (client != null) {
jmsTemplate.send(message.getJMSReplyTo(), new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
TemporaryQueue tq = session.createTemporaryQueue();
session.createConsumer(tq).setMessageListener(new ClientMessageListener(client.getId(), receivingMessageService));
return session.createObjectMessage(new AgentUseQueue(tq.getQueueName()));
}
});
}
} catch (Exception e) {
log.error("Error processing JMS message:", e);
}
}
MessageListener
public class ClientMessageListener implements MessageListener {
private final long clientId;
private final ReceivingMessages agentService;
@Override
public void onMessage(Message message) {
try {
Object o = message.getBody(Object.class);
log.info("Received from clientId {} the message: {}", clientId, o);
.....
if (o instanceof HeartbeatMessage) {
service.heartbeatReceived(clientId, ((HeartbeatMessage) o).getInstances());
}
.....
} catch (Exception e) {
log.error("Could not interpret JMS message: ", e);
}
}
}
application.properties
spring.artemis.mode=native
Customizer
@Component
public class ArtemisCustomizer {
@Bean
public ConnectionFactory jmsConnectionFactory() {
Map<String, Object> transportProperties = new HashMap<String, Object>();
transportProperties.put(TransportConstants.HOST_PROP_NAME, ip);
transportProperties.put(TransportConstants.PORT_PROP_NAME, port);
// log.info("SSL enabled: {}", true);
// transportProperties.put(TransportConstants.SSL_ENABLED_PROP_NAME, true);
// transportProperties.put(TransportConstants.KEYSTORE_PATH_PROP_NAME, keystorePath);
// transportProperties.put(TransportConstants.KEYSTORE_PASSWORD_PROP_NAME, keystorePassword);
return new ActiveMQConnectionFactory(true, new TransportConfiguration(NettyConnectorFactory.class.getName(), transportProperties));
}
}
connection
@Component
public class ConnectionJms {
@Autowired
private JmsTemplate jmsTemplate;
@Autowired
private Properties properties;
@Autowired
private Outbound outbound;
...
@PostConstruct
public void configure() {
jmsTemplate.setReceiveTimeout(60000);
}
@Scheduled(fixedDelay = 30000, initialDelay = 10000)
public void connect() {
if (properties.getJmsQueueSender() == null || properties.getLastMessageReceived() == null || (System.currentTimeMillis() - properties.getLastMessageReceived()) > 120000) {
try {
Message message = this.jmsTemplate.sendAndReceive(properties.getAgentConnectionQueue(), (session) -> {
JMSContext ctx = jmsTemplate.getConnectionFactory().createContext();
TemporaryQueue tq = ctx.createTemporaryQueue();
properties.setJmsQueueReceiver(tq.getQueueName());
ctx.createConsumer(tq).setMessageListener(new MyMessageListener(inbound, properties));
return session.createObjectMessage(new AgentConnection(properties.getJmsQueueReceiver(), properties.getExternalIpAddress(), properties.getInternalIpAddress()));
});
if (message != null) {
AgentUseQueue auq = message.getBody(AgentUseQueue.class);
log.info("Connected. Use this queue to communicate now: {}", auq);
properties.setJmsQueueSender(auq.getQueueName());
properties.setLastMessageReceived(System.currentTimeMillis());
outbound.heartbeat(...);
} else {
log.info("Did not receive any response. Trying again in 15 seconds.");
}
} catch (Exception e) {
log.error("Error sending agent connection message:", e);
}
} else {
outbound.heartbeat(...);
}
}
}
outBound
@Service
@Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)
public class Outbound {
@Autowired
private Properties properties;
@Autowired
private JmsTemplate jmsTemplate;
... methods ...
public void heartbeat(RunningInstanceState[] states) {
log.info("Sending heartbeat. States: {}", new Object[] {states});
send(new HeartbeatMessage(states));
}
public boolean isConnected() {
return properties.getJmsQueueSender() != null;
}
private void send(Serializable message) {
if (isConnected()) {
try {
jmsTemplate.send(properties.getJmsQueueSender(), session -> session.createObjectMessage(message));
} catch (Exception je) {
log.warn("Error on JMS, reseting queues: {}", je.getMessage());
properties.resetJms();
}
}
}
}
EDITED
I Create small maven projects on link http://s000.tinyupload.com/index.php?file_id=07555336945447914472
It is working using HornetQ. The artemis configuration is commented on pom.xml, application.properties and Customizers
Upvotes: 0
Views: 1766
Reputation: 11
The solution was change TemporaryQueue to Queue.
// TemporaryQueue tq = session.createTemporaryQueue(); // only works for HornetQ
String clientQueue = UUID.randomUUID().toString();
Queue q = session.createQueue(clientQueue);
Upvotes: 1