/** * Creates a new {@link JdkSerializationRedisSerializer} using the default class loader. */ public JdkSerializationRedisSerializer() { this(new SerializingConverter(), new DeserializingConverter()); }
@Test public void serializeAndDeserializeString() { SerializingConverter toBytes = new SerializingConverter(); byte[] bytes = toBytes.convert("Testing"); DeserializingConverter fromBytes = new DeserializingConverter(); assertEquals("Testing", fromBytes.convert(bytes)); }
@Override @SuppressWarnings("unchecked") public <T> Message<T> addMessage(final Message<T> message) { UUID id = message.getHeaders().getId(); Assert.notNull(id, "Cannot store messages without an ID header"); final String messageId = getKey(id); final byte[] messageBytes = this.serializer.convert(message); if (logger.isDebugEnabled()) { logger.debug("Inserting message with id key=" + messageId); } try { this.jdbcTemplate.update(getQuery(Query.CREATE_MESSAGE), ps -> { ps.setString(1, messageId); ps.setString(2, this.region); ps.setTimestamp(3, new Timestamp(System.currentTimeMillis())); this.lobHandler.getLobCreator().setBlobAsBytes(ps, 4, messageBytes); }); } catch (DuplicateKeyException e) { if (logger.isDebugEnabled()) { logger.debug("The Message with id [" + id + "] already exists.\n" + "Ignoring INSERT and SELECT existing..."); } return (Message<T>) getMessage(id); } return message; }
byte[] messageBytes = this.serializer.convert(requestMessage); this.lobHandler.getLobCreator().setBlobAsBytes(preparedStatement, 6, messageBytes);
/** * Creates a new {@link JdkSerializationRedisSerializer} using a {@link ClassLoader}. * * @param classLoader the {@link ClassLoader} to use for deserialization. Can be {@literal null}. * @since 1.7 */ public JdkSerializationRedisSerializer(@Nullable ClassLoader classLoader) { this(new SerializingConverter(), new DeserializingConverter(classLoader)); }
@Test public void nonSerializableObject() { SerializingConverter toBytes = new SerializingConverter(); try { toBytes.convert(new Object()); fail("Expected IllegalArgumentException"); } catch (SerializationFailedException e) { assertNotNull(e.getCause()); assertTrue(e.getCause() instanceof IllegalArgumentException); } }
@Override public void setValues(PreparedStatement preparedStatement, Message<?> requestMessage, Object groupId, String region, boolean priorityEnabled) throws SQLException { super.setValues(preparedStatement, requestMessage, groupId, region, priorityEnabled); byte[] messageBytes = this.serializer.convert(requestMessage); this.lobHandler.getLobCreator().setBlobAsBytes(preparedStatement, 6, messageBytes); }
private GenericConversionService createConversionServiceWithBeanClassLoader() { GenericConversionService conversionService = new GenericConversionService(); conversionService.addConverter(Object.class, byte[].class, new SerializingConverter()); conversionService.addConverter(byte[].class, Object.class, new DeserializingConverter(this.classLoader)); return conversionService; }
@Test public void nonSerializableField() { SerializingConverter toBytes = new SerializingConverter(); try { toBytes.convert(new UnSerializable()); fail("Expected SerializationFailureException"); } catch (SerializationFailedException e) { assertNotNull(e.getCause()); assertTrue(e.getCause() instanceof NotSerializableException); } }
@Override public byte[] convert(Object source) { if (source instanceof String) { return ((String) source).getBytes(); } else { return this.serializingConverter.convert(source); } }
private static GenericConversionService createDefaultConversionService() { GenericConversionService converter = new GenericConversionService(); converter.addConverter(Object.class, byte[].class, new SerializingConverter()); converter.addConverter(byte[].class, Object.class, new DeserializingConverter()); return converter; }
.willThrow(new ExpiredIteratorException("Iterator expired")); SerializingConverter serializingConverter = new SerializingConverter(); .withPartitionKey("partition1") .withSequenceNumber("1") .withData(ByteBuffer.wrap(serializingConverter.convert("foo"))), new Record() .withPartitionKey("partition1") .withSequenceNumber("2") .withData(ByteBuffer.wrap(serializingConverter.convert("bar"))))); .withPartitionKey("partition1") .withSequenceNumber("2") .withData(ByteBuffer.wrap(serializingConverter.convert("bar")))));
@Override public byte[] convert(Object source) { if (source instanceof String) { return ((String) source).getBytes(); } else { return this.serializingConverter.convert(source); } }
/** * A converter for serializing messages to byte arrays for storage. * * @param serializer the serializer to set */ @SuppressWarnings("unchecked") public void setSerializer(Serializer<? super Message<?>> serializer) { this.serializer = new SerializingConverter((Serializer<Object>) serializer); }
@Override @SuppressWarnings("unchecked") public <T> Message<T> addMessage(final Message<T> message) { UUID id = message.getHeaders().getId(); Assert.notNull(id, "Cannot store messages without an ID header"); final String messageId = getKey(id); final byte[] messageBytes = this.serializer.convert(message); if (logger.isDebugEnabled()) { logger.debug("Inserting message with id key=" + messageId); } try { this.jdbcTemplate.update(getQuery(Query.CREATE_MESSAGE), ps -> { ps.setString(1, messageId); ps.setString(2, this.region); ps.setTimestamp(3, new Timestamp(System.currentTimeMillis())); this.lobHandler.getLobCreator().setBlobAsBytes(ps, 4, messageBytes); }); } catch (DuplicateKeyException e) { if (logger.isDebugEnabled()) { logger.debug("The Message with id [" + id + "] already exists.\n" + "Ignoring INSERT and SELECT existing..."); } return (Message<T>) getMessage(id); } return message; }
/** * A converter for serializing messages to byte arrays for storage. * @param serializer The serializer to set */ @SuppressWarnings("unchecked") public void setSerializer(Serializer<? super Message<?>> serializer) { Assert.notNull(serializer, "The provided serializer must not be null."); this.serializer = new SerializingConverter((Serializer<Object>) serializer); }
byte[] messageBytes = this.serializer.convert(requestMessage); this.lobHandler.getLobCreator().setBlobAsBytes(preparedStatement, 6, messageBytes);
/** * Create a {@link MessageStore} with all mandatory properties. * @param jdbcOperations a {@link JdbcOperations} * @since 4.3.9 */ public JdbcMessageStore(JdbcOperations jdbcOperations) { Assert.notNull(jdbcOperations, "'dataSource' must not be null"); this.jdbcTemplate = jdbcOperations; this.deserializer = new WhiteListDeserializingConverter(); this.serializer = new SerializingConverter(); }
/** * Instantiate based on the {@link SerializingConverter} with the * {@link org.springframework.core.serializer.DefaultSerializer}. */ public PayloadSerializingTransformer() { doSetConverter(new SerializingConverter()); }
/** * Convenient constructor for configuration use. */ public JdbcChannelMessageStore() { this.deserializer = new WhiteListDeserializingConverter(); this.serializer = new SerializingConverter(); }