我想在我的Java应用程序中使用Windows Azure Service Bus主题和订阅过滤器。我正在使用Windows Azure Plugin for Eclipse with Java (by Microsoft Open Technologies)和Windows Azure SDK 2.0。
我发现Microsoft网站上显示如何使用Java API执行此操作的basic example code无效。
基本示例代码应该如何使用API从Java应用程序以编程方式完成以下操作?
1)获取现有命名空间中现有“TestTopic”的ServiceBusContract对象。
2)使用默认的MatchAll过滤器创建“AllMessages”订阅。此订阅接收发送到其虚拟队列中的“TestTopic”的所有消息。
3)使用SqlFilter过滤器创建“LowMessages”订阅以过滤“MessageNumber< = 3”。此订阅的虚拟队列应仅接收MessageNumber自定义属性值小于或等于3的消息。
4)使用SqlFilter创建“HighMessages”订阅以过滤“MessageNumber< = 3”。此订阅的虚拟队列应仅接收MessageNumber自定义属性值大于3的消息。
5)使用(0< = MessageNumber< 7)
将一批示例代理消息发送到“TestTopic”6)接收所有三个订阅的消息并显示它们已被过滤。
我相信我找到了这个例子不起作用的原因,并且我将修正后的Java代码作为下面的答案包括在内,以便其他人可能也希望看到如何执行此操作的基本示例。
答案 0 :(得分:5)
按照the documentation中的描述从Azure门户获取我的默认管理凭据后,我的应用程序能够获取Service Bus命名空间的ServiceBusContract对象,如下所示:
public class Service
{
private Configuration config;
public Service()
{
String namespace = "jasper";
String issuer = "owner";
String key = "BB9BB9BBBbBBBbBbbBBbB99SS9b+Bb9BbB+bbBBbBB9=";
String serviceBusRootUri = ".servicebus.windows.net";
String wrapRootUri = "-sb.accesscontrol.windows.net/WRAPv0.9";
this.config = ServiceBusConfiguration.configureWithWrapAuthentication(
namespace,
issuer,
key,
serviceBusRootUri,
wrapRootUri);
}
public ServiceBusContract getservice()
{
ServiceBusContract service = ServiceBusService.create(config);
return service;
}
}
然后,可以使用以下Java代码创建对“TestTopic”主题的三个订阅。 “testTopic”必须已存在于“jasper”命名空间中。 “AllMessages”订阅是默认订阅,并接收发送到主题的所有消息。 “LowMessages”和“HighMessages”订阅使用SqlFilter规则根据MessageNumber定制属性的值过滤消息。原始示例代码省略了必须提供规则名称并且必须删除默认规则。如果您不删除默认规则,订阅仍会收到所有邮件。
public class Make_sub_rule
{
public static void main(String[] args) throws ServiceException {
Service creds = new Service();
ServiceBusContract service = creds.getservice();
SubscriptionInfo subInfo = new SubscriptionInfo("AllMessages");
service.createSubscription("TestTopic", subInfo);
System.out.println(subInfo.getName() + " Default Rules");
SubscriptionInfo subInfo1 = new SubscriptionInfo("LowMessages");
CreateSubscriptionResult result1 = service.createSubscription("TestTopic", subInfo1);
RuleInfo ruleInfo1 = new RuleInfo("RULENAME1");
ruleInfo1 = ruleInfo1.withSqlExpressionFilter("MessageNumber <= 3");
CreateRuleResult ruleResult1 =
service.createRule("TestTopic", "LowMessages", ruleInfo1);
service.deleteRule("TestTopic", "LowMessages", "$Default");
System.out.println(subInfo1.getName() + " " + result1.toString());
System.out.println(ruleInfo1.getName() + " " + ruleResult1.toString());
SubscriptionInfo subInfo2 = new SubscriptionInfo("HighMessages");
CreateSubscriptionResult result2 = service.createSubscription("TestTopic", subInfo2);
RuleInfo ruleInfo2 = new RuleInfo("RULENAME2");
ruleInfo2 = ruleInfo2.withSqlExpressionFilter("MessageNumber > 3");
CreateRuleResult ruleResult2 =
service.createRule("TestTopic", "HighMessages", ruleInfo2);
service.deleteRule("TestTopic", "HighMessages", "$Default");
System.out.println(subInfo2.getName() + " " + result2.toString());
System.out.println(ruleInfo2.getName() + " " + ruleResult2.toString());
}
}
以下内容将一批代理消息发送到“TestTopic”并递增MessageNumber自定义属性的值。
public class SendSbMsTopicB
{
public static void main(String[] args) throws ServiceException {
Service creds = new Service();
ServiceBusContract service = creds.getservice();
for (int i=0; i<7; i++)
{
BrokeredMessage message = new BrokeredMessage("Test message" + i);
message.setLabel("Day" + i);
message.setProperty("MessageNumber", i);
message.setProperty("CustomProperty", "CustomTestValue" + i);
service.sendTopicMessage("TestTopic", message);
System.out.println("send MessageNumber " + i + " to topic");
}
}
}
重复运行以下代码以读取“AllMessages”,“LowMessages”或“HighMessages”订阅的虚拟队列(更改subscriptionName的值)。读取每条消息时,将从虚拟队列中删除每条消息,直到该订阅的队列中不再有消息为止。请注意,多个订阅可以接收相同的消息,并且已过滤的订阅不会收到所有消息。
public class GetSbMessSub
{
public static void main(String[] args) throws ServiceException {
Service creds = new Service();
ServiceBusContract service = creds.getservice();
String subscriptionName = "LowMessages";
ReceiveMessageOptions opts = ReceiveMessageOptions.DEFAULT;
opts.setReceiveMode(ReceiveMode.PEEK_LOCK);
ReceiveSubscriptionMessageResult resultQM = service.receiveSubscriptionMessage("TestTopic", subscriptionName, opts);
BrokeredMessage message = resultQM.getValue();
if (message != null && message.getMessageId() != null)
{
try {
System.out.println("Subscription: " + subscriptionName);
System.out.println("MessageNumber: " + message.getProperty("MessageNumber"));
service.deleteMessage(message);}
catch (Exception ex){
System.out.println("Inner exception encountered!");
service.unlockMessage(message);}
}
else {System.out.println("There are no more messages.");}
}
}