如何使用JAVA程序将文件中的消息路由到Azure Event Hub

huangapple go评论76阅读模式
英文:

How to Route message from file to Azure Event Hub Using JAVA Program

问题

//SimpleRouteBuilder

import org.apache.camel.builder.RouteBuilder;

public class SimpleRouteBuilder extends RouteBuilder {

    @Override
    public void configure() throws Exception {

        from("file:d:/InputFolderForCamel").split().tokenize("\n").to("jms:queue:javainuse");

    }
}

//MainClass

SimpleRouteBuilder routeBuilder = new SimpleRouteBuilder();
CamelContext ctx = new DefaultCamelContext();

//configure jms component
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://0.0.0.0:61616");
ctx.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));

try {
    ctx.addRoutes(routeBuilder);
    ctx.start();
    Thread.sleep(5 * 60 * 1000);
    ctx.stop();
} catch (Exception e) {
    e.printStackTrace();
}

这段代码在使用ActiveMQ上工作得很好。

我想用Azure Event Hub编写相同的Java程序。文件必须由Camel读取并路由到Azure Event Hub。

英文:
//SimpleRouteBuilder

import org.apache.camel.builder.RouteBuilder;

public class SimpleRouteBuilder  extends RouteBuilder{

	@Override
	public void configure() throws Exception {
		
		from("file:d:/InputFolderForCamel").split().tokenize("\n").to("jms:queue:javainuse");
		
	}
//MainClass

SimpleRouteBuilder routeBuilder = new SimpleRouteBuilder();
        CamelContext ctx = new DefaultCamelContext();
        
        //configure jms component        
        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://0.0.0.0:61616");
        ctx.addComponent("jms", JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
        
        try {
            ctx.addRoutes(routeBuilder);
            ctx.start();
            Thread.sleep(5 * 60 * 1000);
            ctx.stop();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

It is working fine with ActiveMQ.

I would like to write same java program with Azure Event Hub. File must be readed by Camel and routed to Azure Event Hub

答案1

得分: 1

from("file:d:/InputFolderForCamel").split().tokenize("\n").
  to("azure-eventhubs://[namespace/eventHubName][?options]");

URI格式:azure-eventhubs://[命名空间/事件中心名称][?选项]

如果您提供了connectionString,则不需要提供namespace和eventHubName,因为这些选项已包含在connectionString中。

例如:`azure-eventhubs://camel/camelHub?sharedAccessName={your-SASpolicyName}&sharedAccessKey={your-SASaccessKey}`

如果要使用原始连接字符串,格式如下:`azure-eventhubs:?connectionString=Endpoint=sb://{your-namespace-name}.servicebus.windows.net/;SharedAccessKeyName={your-SASpolicyName};SharedAccessKey={your-SASaccessKey};EntityPath={event-hub-name}`

您可以从Azure门户获取`sharedAccessName`和`sharedAccessKey`的值 -> 您的事件中心命名空间 -> 'Shared Access Policies' 选项卡。

阅读有关Camel事件中心URI的更多信息[此处](https://camel.apache.org/components/latest/azure-eventhubs-component.html#_uri_options)。
英文:
from("file:d:/InputFolderForCamel").split().tokenize("\n").
  to("azure-eventhubs://[namespace/eventHubName][?options]");

URI FORMAT: azure-eventhubs://[namespace/eventHubName][?options]

In case you supply the connectionString, namespace and eventHubName are not required as these options already included in the connectionString.

For example: azure-eventhubs://camel/camelHub?sharedAccessName={your-SASpolicyName}&sharedAccessKey={your-SASaccessKey}

If you want to use raw connection string, it would be azure-eventhubs:?connectionString=Endpoint=sb://{your-namespace-name}.servicebus.windows.net/;SharedAccessKeyName={your-SASpolicyName};SharedAccessKey={your-SASaccessKey};EntityPath={event-hub-name}

You can obtain sharedAccessName and sharedAccessKey values from Azure portal -> Your Event Hub namespace -> 'Shared Access Policies' blade.

如何使用JAVA程序将文件中的消息路由到Azure Event Hub

Read more about event hub URI for Camel here.

huangapple
  • 本文由 发表于 2020年10月12日 17:02:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/64314716.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定