2014-07-21 3 views
2

jersey을 사용하여 개발 된 웹 서비스 집합이 있고 현재 웹 서비스는 JSON 형식의 데이터를 보내고받습니다. 처리 시간과 필요한 메모리면에서 성능을 향상시키기 위해 Protobufavro을 실험하고 있습니다.저지에서 avro 형식으로 데이터를 보내고받는 방법은 무엇입니까?

protobuf을 웹 서비스에 쉽게 통합하는 방법을 보여주는 자습서가 있습니다. 그러나 나는 우리가 avro 형식의 데이터를 jersey을 사용하여 보내고받을 수 있는지에 대한 약간의 아이디어를 제공 할 책이나 튜토리얼을 찾을 수 없습니다.

jersey을 사용하여 avro 형식으로 데이터를 보내고받는 방법을 알고 싶습니다.

답변

1

나는 비슷한 문제가 있으며, 2 년 내에 다른 누구도 해결책을 찾지 못한 것을 알고 놀랐습니다. 우리는 Jersey 2.x를 사용하고 Avro를 처리하기 위해 Provider을 사용했습니다.

코드를 생성하면이 스 니펫이 작동합니다. 그렇지 않은 경우 SpecificDataReader/WriterSpecificRecord 대신 GenericDatumReader/WriterGenericRecord을 사용해야합니다.

또한 Avro 사양은 콘텐츠 유형으로 avro/binary을 사용한다고 밝혔지만 잘못된 유형이므로 변경하려면 6 세의 JIRA ticket이 있습니다.

나는 이것을 단순화하기 위해 이것을 제거 했으므로 오류 처리가 없습니다. avro 바이너리를 생성하는 방법을 모르기 때문에 일반적인 예외를 포착하는 공통 인 ExceptionMapper을 가지고 있다면주의하십시오.

@Provider 
@Consumes("avro/binary") 
@Produces("avro/binary") 
public class AvroProvider <T extends SpecificRecord> implements MessageBodyWriter<T>, MessageBodyReader<T> 
{ 
    public boolean isWriteable(final Class<?> type, final Type genericType, final Annotation[] annotations, 
      final MediaType mediaType) 
    { 
     return SpecificRecord.class.isAssignableFrom(type); 
    } 

    public boolean isReadable(final Class<?> type, final Type genericType, final Annotation[] annotations, 
      final MediaType mediaType) 
    { 
     return true; 
    } 

    @Override 
    public T readFrom(Class<T> type, Type genericType, Annotation[] annotations, MediaType mediaType, 
      MultivaluedMap<String, String> httpHeaders, InputStream entityStream) 
      throws IOException, WebApplicationException 
    { 
     DatumReader<T> reader = new SpecificDatumReader<>(type); 
     Decoder decoder = DecoderFactory.get().binaryDecoder(entityStream, null); 
     return reader.read(null, decoder); 
    } 

    @SuppressWarnings("unchecked") 
    @Override 
    public void writeTo(T message, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, 
      MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) 
      throws IOException, WebApplicationException 
    { 
     DatumWriter<T> datumWriter = new SpecificDatumWriter<>((Class<T>)type); 
     Encoder encoder = EncoderFactory.get().binaryEncoder(entityStream, null); 
     datumWriter.write(message, encoder); 
     encoder.flush(); 
    } 

    @Override 
    public long getSize(T message, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) 
    { 
     return -1; 
    } 

} 
관련 문제