001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.component.directvm;
018
019import org.apache.camel.AsyncEndpoint;
020import org.apache.camel.Consumer;
021import org.apache.camel.Processor;
022import org.apache.camel.Producer;
023import org.apache.camel.component.direct.DirectConsumer;
024import org.apache.camel.impl.DefaultEndpoint;
025import org.apache.camel.spi.HeaderFilterStrategy;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.spi.UriEndpoint;
028import org.apache.camel.spi.UriParam;
029import org.apache.camel.spi.UriPath;
030
031/**
032 * The direct-vm component provides direct, synchronous call to another endpoint from any CamelContext in the same JVM.
033 *
034 * This endpoint can be used to connect existing routes in the same JVM between different CamelContexts.
035 */
036@UriEndpoint(firstVersion = "2.10.0", scheme = "direct-vm", title = "Direct VM", syntax = "direct-vm:name", consumerClass = DirectConsumer.class, label = "core,endpoint")
037public class DirectVmEndpoint extends DefaultEndpoint implements AsyncEndpoint {
038
039    @UriPath(description = "Name of direct-vm endpoint") @Metadata(required = "true")
040    private String name;
041
042    @UriParam(label = "producer", defaultValue = "true")
043    private boolean block = true;
044    @UriParam(label = "producer", defaultValue = "30000")
045    private long timeout = 30000L;
046    @UriParam(label = "producer")
047    private boolean failIfNoConsumers = true;
048    @UriParam(label = "producer,advanced")
049    private HeaderFilterStrategy headerFilterStrategy;
050    @UriParam(label = "advanced", defaultValue = "true")
051    private boolean propagateProperties = true;
052
053    public DirectVmEndpoint(String endpointUri, DirectVmComponent component) {
054        super(endpointUri, component);
055    }
056
057    @Override
058    public DirectVmComponent getComponent() {
059        return (DirectVmComponent) super.getComponent();
060    }
061
062    @Override
063    public Producer createProducer() throws Exception {
064        if (block) {
065            return new DirectVmBlockingProducer(this);
066        } else {
067            return new DirectVmProducer(this);
068        }
069    }
070
071    @Override
072    public Consumer createConsumer(Processor processor) throws Exception {
073        Consumer answer = new DirectVmConsumer(this, new DirectVmProcessor(processor, this));
074        configureConsumer(answer);
075        return answer;
076    }
077
078    @Override
079    public boolean isSingleton() {
080        return true;
081    }
082
083    public DirectVmConsumer getConsumer() {
084        return getComponent().getConsumer(this);
085    }
086
087    public boolean isBlock() {
088        return block;
089    }
090
091    /**
092     * If sending a message to a direct endpoint which has no active consumer,
093     * then we can tell the producer to block and wait for the consumer to become active.
094     */
095    public void setBlock(boolean block) {
096        this.block = block;
097    }
098
099    public long getTimeout() {
100        return timeout;
101    }
102
103    /**
104     * The timeout value to use if block is enabled.
105     */
106    public void setTimeout(long timeout) {
107        this.timeout = timeout;
108    }
109
110    public boolean isFailIfNoConsumers() {
111        return failIfNoConsumers;
112    }
113
114    /**
115     * Whether the producer should fail by throwing an exception, when sending to a Direct-VM endpoint with no active consumers.
116     */
117    public void setFailIfNoConsumers(boolean failIfNoConsumers) {
118        this.failIfNoConsumers = failIfNoConsumers;
119    }
120
121    public HeaderFilterStrategy getHeaderFilterStrategy() {
122        return headerFilterStrategy == null ? getComponent().getHeaderFilterStrategy() : headerFilterStrategy;
123    }
124
125    /**
126     * Sets a {@link HeaderFilterStrategy} that will only be applied on producer endpoints (on both directions: request and response).
127     * <p>Default value: none.</p>
128     */
129    public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
130        this.headerFilterStrategy = headerFilterStrategy;
131    }
132
133    public boolean isPropagateProperties() {
134        return propagateProperties;
135    }
136
137    /**
138     * Whether to propagate or not properties from the producer side to the consumer side, and vice versa.
139     * <p>Default value: true.</p>
140     */
141    public void setPropagateProperties(boolean propagateProperties) {
142        this.propagateProperties = propagateProperties;
143    }
144
145}