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

io.zbus.transport.http.MessageClient Maven / Gradle / Ivy

package io.zbus.transport.http;

import java.util.List;

import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestEncoder;
import io.netty.handler.codec.http.HttpResponseDecoder;
import io.zbus.transport.CodecInitializer;
import io.zbus.transport.CompositeClient;
import io.zbus.transport.EventLoop;
import io.zbus.transport.IoAdaptor;
import io.zbus.transport.Server;
import io.zbus.transport.ServerAddress;
import io.zbus.transport.inproc.InProcClient;
import io.zbus.transport.tcp.TcpClient;
import io.zbus.transport.tcp.TcpClient.HeartbeatMessageBuilder;

public class MessageClient extends CompositeClient{ 
	protected int heartbeatInterval = 60000; //60s
	
	public MessageClient(ServerAddress address, final EventLoop loop){
		initSupport(address, loop);
	}
	
	public MessageClient(String address, final EventLoop loop){ 
		ServerAddress serverAddress = new ServerAddress(address);
		initSupport(serverAddress, loop);
	} 
	
	public MessageClient(IoAdaptor serverIoAdaptor){ 
		support = new InProcClient(serverIoAdaptor);
	} 
	
	public MessageClient(Server server){ 
		support = new InProcClient(server.getIoAdaptor());
	}   
	
	protected void initSupport(ServerAddress address, final EventLoop loop){
		if(address.getServer() != null){
			support = new InProcClient(address.getServer().getIoAdaptor());
			return;
		}
		
		TcpClient tcp = new TcpClient(address, loop);
		support = tcp;
		
		tcp.codec(new CodecInitializer() {
			@Override
			public void initPipeline(List p) {
				p.add(new HttpRequestEncoder()); 
				p.add(new HttpResponseDecoder());  
				p.add(new HttpObjectAggregator(loop.getPackageSizeLimit()));
				p.add(new MessageCodec());
			}
		}); 
		
		tcp.startHeartbeat(heartbeatInterval, new HeartbeatMessageBuilder() { 
			@Override
			public Message build() { 
				Message hbt = new Message();
				hbt.setCommand(Message.HEARTBEAT);
				return hbt;
			} 
		});  
	}
}
 




© 2015 - 2025 Weber Informatics LLC | Privacy Policy