Java Code Examples for org.springframework.beans.DirectFieldAccessor#setPropertyValue()

The following examples show how to use org.springframework.beans.DirectFieldAccessor#setPropertyValue() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InMemoryWebSessionStoreTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void expirationCheckPeriod() {

	DirectFieldAccessor accessor = new DirectFieldAccessor(this.store);
	Map<?,?> sessions = (Map<?, ?>) accessor.getPropertyValue("sessions");
	assertNotNull(sessions);

	// Create 100 sessions
	IntStream.range(0, 100).forEach(i -> insertSession());
	assertEquals(100, sessions.size());

	// Force a new clock (31 min later), don't use setter which would clean expired sessions
	accessor.setPropertyValue("clock", Clock.offset(this.store.getClock(), Duration.ofMinutes(31)));
	assertEquals(100, sessions.size());

	// Create 1 more which forces a time-based check (clock moved forward)
	insertSession();
	assertEquals(1, sessions.size());
}
 
Example 2
Source File: InMemoryWebSessionStoreTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void expirationCheckPeriod() {

	DirectFieldAccessor accessor = new DirectFieldAccessor(this.store);
	Map<?,?> sessions = (Map<?, ?>) accessor.getPropertyValue("sessions");
	assertNotNull(sessions);

	// Create 100 sessions
	IntStream.range(0, 100).forEach(i -> insertSession());
	assertEquals(100, sessions.size());

	// Force a new clock (31 min later), don't use setter which would clean expired sessions
	accessor.setPropertyValue("clock", Clock.offset(this.store.getClock(), Duration.ofMinutes(31)));
	assertEquals(100, sessions.size());

	// Create 1 more which forces a time-based check (clock moved forward)
	insertSession();
	assertEquals(1, sessions.size());
}
 
Example 3
Source File: SubProtocolWebSocketHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
	TestWebSocketSession session1 = new TestWebSocketSession("id1");
	TestWebSocketSession session2 = new TestWebSocketSession("id2");
	session1.setOpen(true);
	session2.setOpen(true);
	session1.setAcceptedProtocol("v12.stomp");
	session2.setAcceptedProtocol("v12.stomp");

	this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session1);
	this.webSocketHandler.afterConnectionEstablished(session2);

	DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
	Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
	DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
	DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));

	long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
	handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
	session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
	session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);

	this.webSocketHandler.start();
	this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));

	assertTrue(session1.isOpen());
	assertNull(session1.getCloseStatus());

	assertFalse(session2.isOpen());
	assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus());

	assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo,
			handlerAccessor.getPropertyValue("lastSessionCheckTime"));
}
 
Example 4
Source File: SubProtocolWebSocketHandlerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
	TestWebSocketSession session1 = new TestWebSocketSession("id1");
	TestWebSocketSession session2 = new TestWebSocketSession("id2");
	session1.setOpen(true);
	session2.setOpen(true);
	session1.setAcceptedProtocol("v12.stomp");
	session2.setAcceptedProtocol("v12.stomp");

	this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session1);
	this.webSocketHandler.afterConnectionEstablished(session2);

	DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
	Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
	DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
	DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));

	long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
	handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
	session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
	session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);

	this.webSocketHandler.start();
	this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));

	assertTrue(session1.isOpen());
	assertNull(session1.getCloseStatus());

	assertFalse(session2.isOpen());
	assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus());

	assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo,
			handlerAccessor.getPropertyValue("lastSessionCheckTime"));
}
 
Example 5
Source File: KinesisTestBinder.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination,
		String group,
		ExtendedConsumerProperties<KinesisConsumerProperties> properties) {

	MessageProducer messageProducer = super.createConsumerEndpoint(destination,
			group, properties);
	DirectFieldAccessor dfa = new DirectFieldAccessor(messageProducer);
	dfa.setPropertyValue("describeStreamBackoff", 10);
	dfa.setPropertyValue("consumerBackoff", 10);
	dfa.setPropertyValue("idleBetweenPolls", 1);
	return messageProducer;
}
 
Example 6
Source File: KinesisBinderProcessorTests.java    From spring-cloud-stream-binder-aws-kinesis with Apache License 2.0 5 votes vote down vote up
@Bean
public MessageProducer kinesisMessageDriverChannelAdapter() {
	KinesisMessageDrivenChannelAdapter kinesisMessageDrivenChannelAdapter =
			new KinesisMessageDrivenChannelAdapter(amazonKinesis(), Processor.OUTPUT);
	kinesisMessageDrivenChannelAdapter.setOutputChannel(fromProcessorChannel());
	kinesisMessageDrivenChannelAdapter.setConverter(null);
	kinesisMessageDrivenChannelAdapter.setBindSourceRecord(true);

	DirectFieldAccessor dfa = new DirectFieldAccessor(kinesisMessageDrivenChannelAdapter);
	dfa.setPropertyValue("describeStreamBackoff", 10);
	dfa.setPropertyValue("consumerBackoff", 10);
	dfa.setPropertyValue("idleBetweenPolls", 1);

	return kinesisMessageDrivenChannelAdapter;
}
 
Example 7
Source File: SubProtocolWebSocketHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void checkSession() throws Exception {
	TestWebSocketSession session1 = new TestWebSocketSession("id1");
	TestWebSocketSession session2 = new TestWebSocketSession("id2");
	session1.setOpen(true);
	session2.setOpen(true);
	session1.setAcceptedProtocol("v12.stomp");
	session2.setAcceptedProtocol("v12.stomp");

	this.webSocketHandler.setProtocolHandlers(Arrays.asList(this.stompHandler));
	this.webSocketHandler.afterConnectionEstablished(session1);
	this.webSocketHandler.afterConnectionEstablished(session2);

	DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(this.webSocketHandler);
	Map<String, ?> map = (Map<String, ?>) handlerAccessor.getPropertyValue("sessions");
	DirectFieldAccessor session1Accessor = new DirectFieldAccessor(map.get("id1"));
	DirectFieldAccessor session2Accessor = new DirectFieldAccessor(map.get("id2"));

	long sixtyOneSecondsAgo = System.currentTimeMillis() - 61 * 1000;
	handlerAccessor.setPropertyValue("lastSessionCheckTime", sixtyOneSecondsAgo);
	session1Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);
	session2Accessor.setPropertyValue("createTime", sixtyOneSecondsAgo);

	this.webSocketHandler.start();
	this.webSocketHandler.handleMessage(session1, new TextMessage("foo"));

	assertTrue(session1.isOpen());
	assertNull(session1.getCloseStatus());

	assertFalse(session2.isOpen());
	assertEquals(CloseStatus.SESSION_NOT_RELIABLE, session2.getCloseStatus());

	assertNotEquals("lastSessionCheckTime not updated", sixtyOneSecondsAgo,
			handlerAccessor.getPropertyValue("lastSessionCheckTime"));
}