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.Consumer;
020import org.apache.camel.Processor;
021import org.apache.camel.Producer;
022import org.apache.camel.component.direct.DirectConsumer;
023import org.apache.camel.impl.DefaultEndpoint;
024import org.apache.camel.spi.Metadata;
025import org.apache.camel.spi.UriEndpoint;
026import org.apache.camel.spi.UriParam;
027import org.apache.camel.spi.UriPath;
028
029/**
030 * The direct-vm endpoint.
031 */
032@UriEndpoint(scheme = "direct-vm", title = "Direct VM", syntax = "direct-vm:name", consumerClass = DirectConsumer.class, label = "core,endpoint")
033public class DirectVmEndpoint extends DefaultEndpoint {
034
035    @UriPath(description = "Name of direct-vm endpoint") @Metadata(required = "true")
036    private String name;
037
038    @UriParam(label = "producer")
039    private boolean block;
040    @UriParam(label = "producer", defaultValue = "30000")
041    private long timeout = 30000L;
042    @UriParam(label = "producer")
043    private boolean failIfNoConsumers = true;
044
045    public DirectVmEndpoint(String endpointUri, DirectVmComponent component) {
046        super(endpointUri, component);
047    }
048
049    @Override
050    public DirectVmComponent getComponent() {
051        return (DirectVmComponent) super.getComponent();
052    }
053
054    @Override
055    public Producer createProducer() throws Exception {
056        if (block) {
057            return new DirectVmBlockingProducer(this);
058        } else {
059            return new DirectVmProducer(this);
060        }
061    }
062
063    @Override
064    public Consumer createConsumer(Processor processor) throws Exception {
065        Consumer answer = new DirectVmConsumer(this, new DirectVmProcessor(processor, this));
066        configureConsumer(answer);
067        return answer;
068    }
069
070    @Override
071    public boolean isSingleton() {
072        return true;
073    }
074
075    public DirectVmConsumer getConsumer() {
076        return getComponent().getConsumer(this);
077    }
078
079    public boolean isBlock() {
080        return block;
081    }
082
083    /**
084     * If sending a message to a direct endpoint which has no active consumer,
085     * then we can tell the producer to block and wait for the consumer to become active.
086     */
087    public void setBlock(boolean block) {
088        this.block = block;
089    }
090
091    public long getTimeout() {
092        return timeout;
093    }
094
095    /**
096     * The timeout value to use if block is enabled.
097     */
098    public void setTimeout(long timeout) {
099        this.timeout = timeout;
100    }
101
102    public boolean isFailIfNoConsumers() {
103        return failIfNoConsumers;
104    }
105
106    /**
107     * Whether the producer should fail by throwing an exception, when sending to a DIRECT-VM endpoint with no active consumers.
108     */
109    public void setFailIfNoConsumers(boolean failIfNoConsumers) {
110        this.failIfNoConsumers = failIfNoConsumers;
111    }
112
113}