All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ar4k.agent.hazelcast.ExternalMessageHandler Maven / Gradle / Ivy

package org.ar4k.agent.hazelcast;

import java.util.HashMap;
import java.util.Map;

import org.ar4k.agent.core.data.AbstractChannel;
import org.springframework.messaging.MessageHeaders;

import com.hazelcast.core.ITopic;
import com.hazelcast.core.Message;
import com.hazelcast.core.MessageListener;

public class ExternalMessageHandler implements MessageListener {

	private ITopic source = null;
	private AbstractChannel target = null;

	public ExternalMessageHandler(ITopic source, AbstractChannel target) {
		this.source = source;
		this.target = target;
	}

	@Override
	public void onMessage(Message message) {
		final HazelcastMessage messageTo = new HazelcastMessage();
		final Map headersMap = new HashMap<>();
		headersMap.put("publishing-member", message.getPublishingMember());
		headersMap.put("publish-time", message.getPublishTime());
		headersMap.put("publish-source", message.getSource());
		final MessageHeaders headers = new MessageHeaders(headersMap);
		messageTo.setHeaders(headers);
		messageTo.setPayload(message.getMessageObject());
		target.getChannel().send(messageTo);
	}

	public ITopic getSource() {
		return source;
	}

	public void setSource(ITopic source) {
		this.source = source;
	}

	public AbstractChannel getTarget() {
		return target;
	}

	public void setTarget(AbstractChannel target) {
		this.target = target;
	}

}