Envio de JMS com dados compactados

1 resposta
Fox_McCloud

Desenvolvi uma classe para testar e demonstrar a serialização / compactação de dados versus a descompactação / desserialização.

Pode ser útil enviar objetos serializados e compactados em mensagens JMS, tornando o tráfego na rede mais leve, e as operações são extremamente rápidas!

Me digam o que vocês acham!

:wink:

obs: efetuei pequenas correções no código, agora está perfeito.

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;


public class ZipTest {

	private final String ZIP_ENTRY_NAME = "dados.bin";
	
	public static void main(String[] args) {
		new ZipTest().testa();
	}
	
	private void testa() {
		List<Dado> lista = preparaDados();
		byte[] dadosSerializados = serializaDados(lista);
		byte[] dadosZipados = zipaDados(dadosSerializados);
		byte[] dadosUnzipados = unzipaDados(dadosZipados);
		imprimeDadosSerializados(dadosUnzipados);
	}

	public List<Dado> preparaDados() {
		List<Dado> lista = new ArrayList<Dado>();
		for(int i=0; i<5; ++i) {
			lista.add(new Dado(i, "Dado "+i));
		}
		return lista;
	}
	
	public byte[] serializaDados(List<Dado> lista) {
		ByteArrayOutputStream objByteOut = null;
		ObjectOutputStream objOut = null;
		
		try{
			objByteOut = new ByteArrayOutputStream();
			objOut = new ObjectOutputStream(objByteOut);
			
			for(Dado dado : lista) {
				objOut.writeObject(dado);
			}
			
			objOut.flush();
			objByteOut.flush();
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(0);
		} finally {
			if(objOut!=null) {
				try{
					objOut.close();
				} catch(IOException e) {
					objOut = null;
				}
			}
			if(objByteOut!=null) {
				try{
					objByteOut.close();
				} catch(IOException e) {
					objByteOut = null;
				}
			}
		}
		
		return objByteOut.toByteArray();
	}
	
	public byte[] zipaDados(byte[] dados) {
		ByteArrayOutputStream zipByteOut = null;
		ZipOutputStream zipOut = null;
		
		try{
			zipByteOut = new ByteArrayOutputStream();
			zipOut = new ZipOutputStream(zipByteOut);
			zipOut.setLevel(Deflater.BEST_COMPRESSION);
			
			ZipEntry entry = new ZipEntry(ZIP_ENTRY_NAME);
			entry.setMethod(ZipEntry.DEFLATED);
			
			zipOut.putNextEntry(entry);
			zipOut.write(dados);
			
			zipOut.flush();
			zipByteOut.flush();
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(0);
		} finally {
			if(zipOut!=null) {
				try {
					zipOut.close();
				} catch(IOException e) {
					zipOut = null;
				}
			}
			if(zipByteOut!=null) {
				try {
					zipByteOut.close();
				} catch(IOException e) {
					zipByteOut = null;
				}
			}
		}
		
		return zipByteOut.toByteArray();
	}
	
	public byte[] unzipaDados(byte[] dadosZipados) {
		ByteArrayOutputStream byteOut = null;
		ByteArrayInputStream zipByteIn = null;
		ZipInputStream zipIn = null;
		
		try{
			zipByteIn = new ByteArrayInputStream(dadosZipados);
			zipIn = new ZipInputStream(zipByteIn);
			byteOut = new ByteArrayOutputStream();
			
			zipIn.getNextEntry();
			
			byte[] buffer = new byte[1024];
			int lidos = 0;
			
			while((lidos = zipIn.read(buffer)) != -1) {
				byteOut.write(buffer, 0, lidos);
			}
			
			byteOut.flush();
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(0);
		} finally {
			try {
				zipIn.close();
			} catch (IOException e) {
				zipIn = null;
			}
			try {
				zipByteIn.close();
			} catch (IOException e) {
				zipByteIn = null;
			}
			try {
				byteOut.close();
			} catch (IOException e) {
				byteOut = null;
			}
		}
		
		return byteOut.toByteArray();
	}
	
	public void imprimeDadosSerializados(byte[] dados) {
		ByteArrayInputStream byteIn = null;
		ObjectInputStream objIn = null;
		
		try {
			byteIn = new ByteArrayInputStream(dados);
			objIn = new ObjectInputStream(byteIn);
			
			Dado dado = null;
			
			System.out.println("Dados lidos:");
			
			while(true) {
				try {
					dado = (Dado)objIn.readObject();
					System.out.println(dado.toString());
				} catch(EOFException e) {
					break;
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
			System.exit(0);
		} finally {
			try {
				objIn.close();
			} catch (IOException e) {
				objIn = null;
			}
			try {
				byteIn.close();
			} catch (IOException e) {
				byteIn = null;
			}
		}
	}
	
	private static class Dado implements Serializable {
		
		private static final long serialVersionUID = 1L;
		
		private int info1;
		private String info2;
		
		public Dado() {
			super();
		}
		
		public Dado(int info1, String info2) {
			this.info1 = info1;
			this.info2 = info2;
		}
		
		public int getInfo1() {
			return info1;
		}
		
		public void setInfo1(int info1) {
			this.info1 = info1;
		}
		
		public String getInfo2() {
			return info2;
		}
		
		public void setInfo2(String info2) {
			this.info2 = info2;
		}
		
		public String toString() {
			return "info1: "+info1+", info2: "+info2;
		}
		
	}
	
}

1 Resposta

Joao.Gabriel

Pessoal,

Desculpe por ressuscitar o tópico, mas gostaria apenas de uma opinião com relação à abordagem do Fox McCloud. É uma boa alternativa compactar mensagens? É uma técnica comumente praticada? E uma última dúvida, os Brokers disponíveis já fazem algo parecido?

Att.

Criado 27 de abril de 2009
Ultima resposta 14 de fev. de 2012
Respostas 1
Participantes 2