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", 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
043    public DirectVmEndpoint(String endpointUri, DirectVmComponent component) {
044        super(endpointUri, component);
045    }
046
047    @Override
048    public DirectVmComponent getComponent() {
049        return (DirectVmComponent) super.getComponent();
050    }
051
052    @Override
053    public Producer createProducer() throws Exception {
054        if (block) {
055            return new DirectVmBlockingProducer(this);
056        } else {
057            return new DirectVmProducer(this);
058        }
059    }
060
061    @Override
062    public Consumer createConsumer(Processor processor) throws Exception {
063        Consumer answer = new DirectVmConsumer(this, new DirectVmProcessor(processor, this));
064        configureConsumer(answer);
065        return answer;
066    }
067
068    @Override
069    public boolean isSingleton() {
070        return true;
071    }
072
073    public DirectVmConsumer getConsumer() {
074        return getComponent().getConsumer(this);
075    }
076
077    public boolean isBlock() {
078        return block;
079    }
080
081    /**
082     * If sending a message to a direct endpoint which has no active consumer,
083     * then we can tell the producer to block and wait for the consumer to become active.
084     */
085    public void setBlock(boolean block) {
086        this.block = block;
087    }
088
089    public long getTimeout() {
090        return timeout;
091    }
092
093    /**
094     * The timeout value to use if block is enabled.
095     */
096    public void setTimeout(long timeout) {
097        this.timeout = timeout;
098    }
099}