Example usage for org.springframework.messaging.support MessageBuilder withPayload

List of usage examples for org.springframework.messaging.support MessageBuilder withPayload

Introduction

In this page you can find the example usage for org.springframework.messaging.support MessageBuilder withPayload.

Prototype

public static <T> MessageBuilder<T> withPayload(T payload) 

Source Link

Document

Create a new builder for a message with the given payload.

Usage

From source file:org.springframework.messaging.support.converter.AbstractMessageConverter.java

@Override
public final Message<?> toMessage(Object payload, MessageHeaders headers) {
    if (!canConvertTo(payload, headers)) {
        return null;
    }//ww w.  jav a 2 s  . c o  m
    payload = convertToInternal(payload, headers);
    MessageBuilder<?> builder = MessageBuilder.withPayload(payload);
    if (headers != null) {
        builder.copyHeaders(headers);
    }
    MimeType mimeType = getDefaultContentType(payload);
    if (mimeType != null) {
        builder.setHeaderIfAbsent(MessageHeaders.CONTENT_TYPE, mimeType);
    }
    return builder.build();
}

From source file:org.springframework.samples.portfolio.web.tomcat.TestStompClient.java

public void subscribe(String destination, MessageHandler messageHandler) {

    String id = String.valueOf(this.subscriptionIndex.getAndIncrement());
    this.subscriptionHandlers.put(id, messageHandler);

    StompHeaderAccessor headers = StompHeaderAccessor.create(StompCommand.SUBSCRIBE);
    headers.setSubscriptionId(id);//  www  .  j a  va  2 s .  co  m
    headers.setDestination(destination);

    Message<byte[]> message = MessageBuilder.withPayload(new byte[0]).setHeaders(headers).build();
    byte[] bytes = encoder.encode(message);
    try {
        this.session.sendMessage(new TextMessage(new String(bytes, DEFAULT_CHARSET)));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:org.springframework.statemachine.ensemble.DistributedStateMachine.java

@Override
public boolean sendEvent(E event) {
    return sendEvent(MessageBuilder.withPayload(event).build());
}

From source file:org.springframework.statemachine.EnumStateMachineTests.java

@Test
public void testSimpleStateSwitch() {
    PseudoState<TestStates, TestEvents> pseudoState = new DefaultPseudoState<TestStates, TestEvents>(
            PseudoStateKind.INITIAL);//from   w w  w  .jav  a  2s  .c  om
    State<TestStates, TestEvents> stateSI = new EnumState<TestStates, TestEvents>(TestStates.SI, pseudoState);
    State<TestStates, TestEvents> stateS1 = new EnumState<TestStates, TestEvents>(TestStates.S1);
    State<TestStates, TestEvents> stateS2 = new EnumState<TestStates, TestEvents>(TestStates.S2);
    State<TestStates, TestEvents> stateS3 = new EnumState<TestStates, TestEvents>(TestStates.S3);

    Collection<State<TestStates, TestEvents>> states = new ArrayList<State<TestStates, TestEvents>>();
    states.add(stateSI);
    states.add(stateS1);
    states.add(stateS2);
    states.add(stateS3);

    Collection<Transition<TestStates, TestEvents>> transitions = new ArrayList<Transition<TestStates, TestEvents>>();

    Collection<Action<TestStates, TestEvents>> actionsFromSIToS1 = new ArrayList<Action<TestStates, TestEvents>>();
    actionsFromSIToS1.add(new LoggingAction("actionsFromSIToS1"));
    DefaultExternalTransition<TestStates, TestEvents> transitionFromSIToS1 = new DefaultExternalTransition<TestStates, TestEvents>(
            stateSI, stateS1, actionsFromSIToS1, TestEvents.E1, null,
            new EventTrigger<TestStates, TestEvents>(TestEvents.E1));

    Collection<Action<TestStates, TestEvents>> actionsFromS1ToS2 = new ArrayList<Action<TestStates, TestEvents>>();
    actionsFromS1ToS2.add(new LoggingAction("actionsFromS1ToS2"));
    DefaultExternalTransition<TestStates, TestEvents> transitionFromS1ToS2 = new DefaultExternalTransition<TestStates, TestEvents>(
            stateS1, stateS2, actionsFromS1ToS2, TestEvents.E2, null,
            new EventTrigger<TestStates, TestEvents>(TestEvents.E2));

    Collection<Action<TestStates, TestEvents>> actionsFromS2ToS3 = new ArrayList<Action<TestStates, TestEvents>>();
    actionsFromS1ToS2.add(new LoggingAction("actionsFromS2ToS3"));
    DefaultExternalTransition<TestStates, TestEvents> transitionFromS2ToS3 = new DefaultExternalTransition<TestStates, TestEvents>(
            stateS2, stateS3, actionsFromS2ToS3, TestEvents.E3, null,
            new EventTrigger<TestStates, TestEvents>(TestEvents.E3));

    transitions.add(transitionFromSIToS1);
    transitions.add(transitionFromS1ToS2);
    transitions.add(transitionFromS2ToS3);

    SyncTaskExecutor taskExecutor = new SyncTaskExecutor();
    BeanFactory beanFactory = new DefaultListableBeanFactory();
    ObjectStateMachine<TestStates, TestEvents> machine = new ObjectStateMachine<TestStates, TestEvents>(states,
            transitions, stateSI);
    machine.setTaskExecutor(taskExecutor);
    machine.setBeanFactory(beanFactory);
    machine.afterPropertiesSet();
    machine.start();

    State<TestStates, TestEvents> initialState = machine.getInitialState();
    assertThat(initialState, is(stateSI));

    State<TestStates, TestEvents> state = machine.getState();
    assertThat(state, is(stateSI));

    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build());
    state = machine.getState();
    assertThat(state, is(stateS1));

    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E2).build());
    state = machine.getState();
    assertThat(state, is(stateS2));

    // not processed
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build());
    state = machine.getState();
    assertThat(state, is(stateS2));

    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E3).build());
    state = machine.getState();
    assertThat(state, is(stateS3));
}

From source file:org.springframework.statemachine.EnumStateMachineTests.java

@Test
public void testDeferredEvents() {
    PseudoState<TestStates, TestEvents> pseudoState = new DefaultPseudoState<TestStates, TestEvents>(
            PseudoStateKind.INITIAL);/*w ww  .  j a va  2  s.  co  m*/

    Collection<TestEvents> deferred = new ArrayList<TestEvents>();
    deferred.add(TestEvents.E2);
    deferred.add(TestEvents.E3);

    // states
    State<TestStates, TestEvents> stateSI = new EnumState<TestStates, TestEvents>(TestStates.SI, deferred, null,
            null, pseudoState);
    State<TestStates, TestEvents> stateS1 = new EnumState<TestStates, TestEvents>(TestStates.S1);
    State<TestStates, TestEvents> stateS2 = new EnumState<TestStates, TestEvents>(TestStates.S2);
    State<TestStates, TestEvents> stateS3 = new EnumState<TestStates, TestEvents>(TestStates.S3);

    Collection<State<TestStates, TestEvents>> states = new ArrayList<State<TestStates, TestEvents>>();
    states.add(stateSI);
    states.add(stateS1);
    states.add(stateS2);
    states.add(stateS3);

    // transitions
    Collection<Transition<TestStates, TestEvents>> transitions = new ArrayList<Transition<TestStates, TestEvents>>();

    Collection<Action<TestStates, TestEvents>> actionsFromSIToS1 = new ArrayList<Action<TestStates, TestEvents>>();
    actionsFromSIToS1.add(new LoggingAction("actionsFromSIToS1"));
    DefaultExternalTransition<TestStates, TestEvents> transitionFromSIToS1 = new DefaultExternalTransition<TestStates, TestEvents>(
            stateSI, stateS1, actionsFromSIToS1, TestEvents.E1, null,
            new EventTrigger<TestStates, TestEvents>(TestEvents.E1));

    Collection<Action<TestStates, TestEvents>> actionsFromS1ToS2 = new ArrayList<Action<TestStates, TestEvents>>();
    actionsFromS1ToS2.add(new LoggingAction("actionsFromS1ToS2"));
    DefaultExternalTransition<TestStates, TestEvents> transitionFromS1ToS2 = new DefaultExternalTransition<TestStates, TestEvents>(
            stateS1, stateS2, actionsFromS1ToS2, TestEvents.E2, null,
            new EventTrigger<TestStates, TestEvents>(TestEvents.E2));

    Collection<Action<TestStates, TestEvents>> actionsFromS2ToS3 = new ArrayList<Action<TestStates, TestEvents>>();
    actionsFromS1ToS2.add(new LoggingAction("actionsFromS2ToS3"));
    DefaultExternalTransition<TestStates, TestEvents> transitionFromS2ToS3 = new DefaultExternalTransition<TestStates, TestEvents>(
            stateS2, stateS3, actionsFromS2ToS3, TestEvents.E3, null,
            new EventTrigger<TestStates, TestEvents>(TestEvents.E3));

    transitions.add(transitionFromSIToS1);
    transitions.add(transitionFromS1ToS2);
    transitions.add(transitionFromS2ToS3);

    // create machine
    SyncTaskExecutor taskExecutor = new SyncTaskExecutor();
    BeanFactory beanFactory = new DefaultListableBeanFactory();
    ObjectStateMachine<TestStates, TestEvents> machine = new ObjectStateMachine<TestStates, TestEvents>(states,
            transitions, stateSI);
    machine.setTaskExecutor(taskExecutor);
    machine.setBeanFactory(beanFactory);
    machine.afterPropertiesSet();
    machine.start();

    State<TestStates, TestEvents> initialState = machine.getInitialState();
    assertThat(initialState, is(stateSI));

    State<TestStates, TestEvents> state = machine.getState();
    assertThat(state, is(stateSI));

    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E2).build());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E3).build());
    state = machine.getState();
    assertThat(state, is(stateSI));

    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build());
    state = machine.getState();
    assertThat(state, is(stateS3));
}

From source file:org.springframework.statemachine.EnumStateMachineTests.java

@Test
public void testInternalTransitions() {
    PseudoState<TestStates, TestEvents> pseudoState = new DefaultPseudoState<TestStates, TestEvents>(
            PseudoStateKind.INITIAL);//w w  w  .  j ava 2  s  .  c o  m
    State<TestStates, TestEvents> stateSI = new EnumState<TestStates, TestEvents>(TestStates.SI, pseudoState);

    Collection<State<TestStates, TestEvents>> states = new ArrayList<State<TestStates, TestEvents>>();
    states.add(stateSI);

    Collection<Action<TestStates, TestEvents>> actionsInSI = new ArrayList<Action<TestStates, TestEvents>>();
    actionsInSI.add(new LoggingAction("actionsInSI"));
    DefaultInternalTransition<TestStates, TestEvents> transitionInternalSI = new DefaultInternalTransition<TestStates, TestEvents>(
            stateSI, actionsInSI, TestEvents.E1, null, new EventTrigger<TestStates, TestEvents>(TestEvents.E1));

    // transitions
    Collection<Transition<TestStates, TestEvents>> transitions = new ArrayList<Transition<TestStates, TestEvents>>();
    transitions.add(transitionInternalSI);

    SyncTaskExecutor taskExecutor = new SyncTaskExecutor();
    BeanFactory beanFactory = new DefaultListableBeanFactory();
    ObjectStateMachine<TestStates, TestEvents> machine = new ObjectStateMachine<TestStates, TestEvents>(states,
            transitions, stateSI);
    machine.setTaskExecutor(taskExecutor);
    machine.setBeanFactory(beanFactory);
    machine.afterPropertiesSet();
    machine.start();

    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build());
}

From source file:org.springframework.statemachine.listener.ListenerTests.java

@Test
public void testStateEvents() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config1.class);
    assertTrue(ctx.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
    @SuppressWarnings("unchecked")
    ObjectStateMachine<TestStates, TestEvents> machine = ctx
            .getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
    machine.start();/*  w ww .java  2  s. c om*/

    TestStateMachineListener listener = new TestStateMachineListener();
    machine.addStateListener(listener);

    assertThat(machine, notNullValue());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).setHeader("foo", "jee1").build());
    assertThat(listener.states.size(), is(1));
    assertThat(listener.states.get(0).from.getIds(), contains(TestStates.S1));
    assertThat(listener.states.get(0).to.getIds(), contains(TestStates.S2));
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E2).setHeader("foo", "jee2").build());
    assertThat(listener.states.size(), is(2));
    assertThat(listener.states.get(1).from.getIds(), contains(TestStates.S2));
    assertThat(listener.states.get(1).to.getIds(), contains(TestStates.S3));
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E4).setHeader("foo", "jee2").build());
    assertThat(listener.states.size(), is(2));

    ctx.close();
}

From source file:org.springframework.statemachine.StateMachineTests.java

@Test
public void testLoggingEvents() {
    context.register(Config1.class);
    context.refresh();/*from w w  w  . j  av  a2s.  co  m*/
    assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
    @SuppressWarnings("unchecked")
    ObjectStateMachine<TestStates, TestEvents> machine = context
            .getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, ObjectStateMachine.class);
    assertThat(machine, notNullValue());
    machine.start();
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).setHeader("foo", "jee1").build());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E2).setHeader("foo", "jee2").build());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E4).setHeader("foo", "jee2").build());
}

From source file:org.springframework.statemachine.StateMachineTests.java

@Test
public void testStringStatesAndEvents() throws Exception {
    context.register(Config4.class);
    context.refresh();//ww w  . j  a  v a 2 s .c o m
    assertTrue(context.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
    @SuppressWarnings("unchecked")
    StateMachine<String, String> machine = context.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE,
            StateMachine.class);

    TestListener2 listener = new TestListener2();
    machine.addStateListener(listener);

    assertThat(machine, notNullValue());
    machine.start();
    listener.reset(1);
    machine.sendEvent(MessageBuilder.withPayload("E1").setHeader("foo", "jee1").build());
    assertThat(listener.stateChangedLatch.await(2, TimeUnit.SECONDS), is(true));
    assertThat(listener.stateChangedCount, is(1));
    assertThat(machine.getState().getIds(), containsInAnyOrder("S1"));
}

From source file:org.springframework.statemachine.StateMachineTests.java

@Test
public void testBackToItself() {
    context.register(BaseConfig.class, Config5.class);
    context.refresh();//from  w w w.jav a  2 s. c om
    @SuppressWarnings("unchecked")
    StateMachine<TestStates, TestEvents> machine = context
            .getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, StateMachine.class);
    assertThat(machine, notNullValue());
    TestStateEntryExitListener listener = new TestStateEntryExitListener();
    machine.addStateListener(listener);
    machine.start();
    assertThat(machine.getState().getIds(), contains(TestStates.SI));
    listener.reset();
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).build());
    assertThat(machine.getState().getIds(), contains(TestStates.SI));
    assertThat(listener.exited.size(), is(1));
    assertThat(listener.entered.size(), is(1));
}