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.xd.reactor.BroadcasterMessageHandler.java

/**
 * Construct a new BroadcasterMessageHandler given the reactor based Processor to delegate
 * processing to./*  ww  w. j a  v  a 2s  .c o m*/
 *
 * @param processor The stream based reactor processor
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public BroadcasterMessageHandler(Processor processor) {
    Assert.notNull(processor, "processor cannot be null.");
    this.reactorProcessor = processor;
    Environment.initializeIfEmpty(); // This by default uses SynchronousDispatcher
    Method method = ReflectionUtils.findMethod(this.reactorProcessor.getClass(), "process", Stream.class);
    this.inputType = ResolvableType.forMethodParameter(method, 0).getNested(2);

    //Stream with a SynchronousDispatcher as this handler is called by Message Listener managed threads
    this.stream = SerializedBroadcaster.create();

    //user defined stream processing
    Stream<?> outputStream = processor.process(stream);

    //Simple log error handling
    outputStream.when(Throwable.class, new Consumer<Throwable>() {
        @Override
        public void accept(Throwable throwable) {
            logger.error(throwable);
        }
    });

    this.control = outputStream.consume(new Consumer<Object>() {
        @Override
        public void accept(Object outputObject) {
            if (ClassUtils.isAssignable(Message.class, outputObject.getClass())) {
                getOutputChannel().send((Message) outputObject);
            } else {
                getOutputChannel().send(MessageBuilder.withPayload(outputObject).build());
            }
        }
    });

    if (logger.isDebugEnabled()) {
        logger.debug(control.debug());
    }

}

From source file:org.springframework.xd.reactor.MultipleBroadcasterMessageHandler.java

private Broadcaster<Object> getBroadcaster(Message<?> message) {
    final Object idToUse = partitionExpression.getValue(evaluationContext, message, Object.class);
    if (logger.isDebugEnabled()) {
        logger.debug("Partition Expression evaluated to " + idToUse);
    }/*  w w w .j a  v  a  2  s  .  com*/
    Broadcaster<Object> broadcaster = broadcasterMap.get(idToUse);
    if (broadcaster == null) {
        Broadcaster<Object> existingBroadcaster = broadcasterMap.putIfAbsent(idToUse,
                SerializedBroadcaster.create());
        if (existingBroadcaster == null) {
            broadcaster = broadcasterMap.get(idToUse);
            //user defined stream processing
            Stream<?> outputStream = processor.process(broadcaster);

            final Control control = outputStream.consume(new Consumer<Object>() {
                @Override
                public void accept(Object outputObject) {
                    if (ClassUtils.isAssignable(Message.class, outputObject.getClass())) {
                        getOutputChannel().send((Message) outputObject);
                    } else {
                        getOutputChannel().send(MessageBuilder.withPayload(outputObject).build());
                    }
                }
            });

            outputStream.when(Throwable.class, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) {
                    logger.error(throwable);
                    broadcasterMap.remove(idToUse);
                }
            });

            broadcaster.observeComplete(new Consumer<Void>() {
                @Override
                public void accept(Void aVoid) {
                    logger.error("Consumer completed for [" + control + "]");
                    broadcasterMap.remove(idToUse);
                }
            });

            controlsMap.put(idToUse, control);

            if (logger.isDebugEnabled()) {
                logger.debug(control.debug());
            }
        } else {
            broadcaster = existingBroadcaster;
        }
    }
    return broadcaster;
}

From source file:org.springframework.xd.reactor.SynchronousDispatcherMessageHandler.java

/**
 * Construct a new SynchronousDispatcherMessageHandler given the reactor based Processor to delegate
 * processing to.//from   ww w  .  j a va 2 s. com
 *
 * @param processor The stream based reactor processor
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public SynchronousDispatcherMessageHandler(Processor processor) {
    Assert.notNull(processor, "processor cannot be null.");
    this.reactorProcessor = processor;
    Environment.initializeIfEmpty(); // This by default uses SynchronousDispatcher
    Method method = ReflectionUtils.findMethod(this.reactorProcessor.getClass(), "process", Stream.class);
    this.inputType = ResolvableType.forMethodParameter(method, 0).getNested(2);

    //Stream with a SynchronousDispatcher as this handler is called by Message Listener managed threads
    this.stream = Streams.broadcast();

    //user defined stream processing
    Stream<?> outputStream = processor.process(stream);

    //Simple log error handling
    outputStream.when(Throwable.class, new Consumer<Throwable>() {
        @Override
        public void accept(Throwable throwable) {
            logger.error(throwable);
        }
    });

    this.consume = outputStream.consume(new Consumer<Object>() {
        @Override
        public void accept(Object outputObject) {
            if (ClassUtils.isAssignable(Message.class, outputObject.getClass())) {
                getOutputChannel().send((Message) outputObject);
            } else {
                //TODO handle copy of header values when possible
                getOutputChannel().send(MessageBuilder.withPayload(outputObject).build());
            }
            //TODO handle Void
        }
    });

    if (logger.isDebugEnabled()) {
        logger.debug(consume.debug());
    }

}

From source file:org.springframework.xd.rxjava.MultipleSubjectMessageHandler.java

private Subject getSubject(Message message) {
    final Object idToUse = partitionExpression.getValue(evaluationContext, message, Object.class);
    if (logger.isDebugEnabled()) {
        logger.debug("Partition Expression evaluated to " + idToUse);
    }/* w w  w  .ja v  a2s  .  com*/
    Subject subject = subjectMap.get(idToUse);
    if (subject == null) {
        Subject existingSubject = subjectMap.putIfAbsent(idToUse, PublishSubject.create());
        if (existingSubject == null) {
            subject = subjectMap.get(idToUse);
            //user defined stream processing
            Observable<?> outputStream = processor.process(subject);

            final Subscription subscription = outputStream.subscribe(new Action1<Object>() {
                @Override
                public void call(Object outputObject) {
                    if (ClassUtils.isAssignable(Message.class, outputObject.getClass())) {
                        getOutputChannel().send((Message) outputObject);
                    } else {
                        getOutputChannel().send(MessageBuilder.withPayload(outputObject).build());
                    }
                }
            }, new Action1<Throwable>() {
                @Override
                public void call(Throwable throwable) {
                    logger.error(throwable);
                    subjectMap.remove(idToUse);
                }
            });

            outputStream.doOnCompleted(new Action0() {
                @Override
                public void call() {
                    logger.error("Subscription close for [" + subscription + "]");
                    subjectMap.remove(idToUse);
                }
            });

            subscriptionMap.put(idToUse, subscription);
        } else {
            subject = existingSubject;
        }
    }
    return subject;

}

From source file:org.springframework.xd.rxjava.SubjectMessageHandler.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public SubjectMessageHandler(Processor processor) {
    Assert.notNull(processor, "processor cannot be null.");
    this.processor = processor;
    Method method = ReflectionUtils.findMethod(this.processor.getClass(), "process", Observable.class);
    this.inputType = ResolvableType.forMethodParameter(method, 0).getNested(2);
    subject = new SerializedSubject(PublishSubject.create());
    Observable<?> outputStream = processor.process(subject);
    subscription = outputStream.subscribe(new Action1<Object>() {
        @Override/*ww w  .j a  v a 2s  .co m*/
        public void call(Object outputObject) {
            if (ClassUtils.isAssignable(Message.class, outputObject.getClass())) {
                getOutputChannel().send((Message) outputObject);
            } else {
                getOutputChannel().send(MessageBuilder.withPayload(outputObject).build());
            }
        }
    }, new Action1<Throwable>() {
        @Override
        public void call(Throwable throwable) {
            logger.error(throwable);
        }
    }, new Action0() {
        @Override
        public void call() {
            logger.error("Subscription close for [" + subscription + "]");
        }
    });
}

From source file:org.springframework.yarn.am.cluster.AbstractContainerClusterAppmaster.java

@Override
public void startContainerCluster(String id) {
    ContainerCluster cluster = clusters.get(id);
    if (cluster != null) {
        StateMachine<State<ClusterState, ClusterEvent>, ClusterEvent> stateMachine = cluster.getStateMachine();
        stateMachine.sendEvent(ClusterEvent.START);
        stateMachine.sendEvent(//from  w ww .  ja  v a 2s . c o m
                MessageBuilder.withPayload(ClusterEvent.CONFIGURE).setHeader("containercluster", cluster)
                        .setHeader("appmaster", AbstractContainerClusterAppmaster.this).build());
    }
}

From source file:org.springframework.yarn.am.cluster.AbstractContainerClusterAppmaster.java

@Override
public void stopContainerCluster(String id) {
    ContainerCluster cluster = clusters.get(id);
    if (cluster != null) {
        StateMachine<State<ClusterState, ClusterEvent>, ClusterEvent> stateMachine = cluster.getStateMachine();
        stateMachine//w  w w  .  jav a  2s  . c  om
                .sendEvent(MessageBuilder.withPayload(ClusterEvent.STOP).setHeader("containercluster", cluster)
                        .setHeader("appmaster", AbstractContainerClusterAppmaster.this).build());
    }
}

From source file:org.springframework.yarn.am.cluster.AbstractContainerClusterAppmaster.java

@Override
public void destroyContainerCluster(String id) {
    ContainerCluster cluster = clusters.get(id);
    if (cluster != null) {
        StateMachine<State<ClusterState, ClusterEvent>, ClusterEvent> stateMachine = cluster.getStateMachine();
        stateMachine.sendEvent(//from   w  w  w .  j a va 2  s  .  c om
                MessageBuilder.withPayload(ClusterEvent.DESTROY).setHeader("containercluster", cluster)
                        .setHeader("appmaster", AbstractContainerClusterAppmaster.this).build());
    }
}

From source file:org.springframework.yarn.am.cluster.AbstractContainerClusterAppmaster.java

@Override
public void modifyContainerCluster(String id, ProjectionData data) {
    ContainerCluster cluster = clusters.get(id);
    if (cluster != null) {
        StateMachine<State<ClusterState, ClusterEvent>, ClusterEvent> stateMachine = cluster.getStateMachine();
        stateMachine.sendEvent(MessageBuilder.withPayload(ClusterEvent.CONFIGURE)
                .setHeader("projectiondata", data).setHeader("containercluster", cluster)
                .setHeader("appmaster", AbstractContainerClusterAppmaster.this).build());
    }/*from w  ww.j  a v a 2  s  .c  o m*/
}

From source file:org.springframework.yarn.am.cluster.AbstractContainerClusterAppmaster.java

private void requestAllocationForAll() {
    for (ContainerCluster cluster : clusters.values()) {
        StateMachine<State<ClusterState, ClusterEvent>, ClusterEvent> stateMachine = cluster.getStateMachine();
        stateMachine.sendEvent(//from  ww  w. java  2  s  .  c  om
                MessageBuilder.withPayload(ClusterEvent.CONFIGURE).setHeader("containercluster", cluster)
                        .setHeader("appmaster", AbstractContainerClusterAppmaster.this).build());
    }

}