Skip to content

Commit

Permalink
Updating samples (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtaubensee authored Apr 4, 2017
1 parent d3b107d commit 9f9d4be
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package com.microsoft.azure.servicebus.samples;

import java.time.Duration;
import java.util.concurrent.CompletableFuture;

import com.microsoft.azure.servicebus.ClientFactory;
import com.microsoft.azure.servicebus.ExceptionPhase;
import com.microsoft.azure.servicebus.IMessage;
import com.microsoft.azure.servicebus.IMessageHandler;
import com.microsoft.azure.servicebus.IMessageReceiver;
import com.microsoft.azure.servicebus.MessageHandlerOptions;
import com.microsoft.azure.servicebus.QueueClient;
import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;

public class ReceiveSample {

private static final String ENVIRONMENT_VARIABLE_NAME = "azure-service-bus-java/connectionstring";
private static IMessageReceiver receiver;
private static QueueClient queueClient;

public static void main(String[] args) throws Exception {
System.out.println("Begining receive sample.");
Expand All @@ -23,19 +27,27 @@ public static void main(String[] args) throws Exception {
throw new Exception("Could not read environment variable: " + ENVIRONMENT_VARIABLE_NAME);
}

ConnectionStringBuilder csb = new ConnectionStringBuilder(envVar);
receiver = ClientFactory.createMessageReceiverFromConnectionStringBuilder(csb, ReceiveMode.PeekLock);

QueueClient queueClient = new QueueClient(envVar, ReceiveMode.PeekLock);
receiveMessages();
Thread.sleep(60 * 1000);
queueClient.close();
System.out.println("Receive sample completed.");
}

private static void receiveMessages() throws InterruptedException, ServiceBusException {
while (true) {
IMessage receivedMessage = receiver.receive(Duration.ofMinutes(1));
System.out.println(new String(receivedMessage.getContent()));
receiver.complete(receivedMessage.getLockToken());
}
private static void receiveMessages() throws InterruptedException, ServiceBusException {
queueClient.registerMessageHandler(new IMessageHandler() {

@Override
public CompletableFuture<Void> onMessageAsync(IMessage message) {
System.out.println(new String(message.getContent()));
return CompletableFuture.completedFuture(null);
}

@Override
public void notifyException(Throwable exception, ExceptionPhase phase) {
System.out.println(phase + " encountered exception:" + exception.getMessage());
}
}, new MessageHandlerOptions(1, true, Duration.ofMinutes(2)));
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.microsoft.azure.servicebus.samples;

import com.microsoft.azure.servicebus.Message;
import com.microsoft.azure.servicebus.ClientFactory;
import com.microsoft.azure.servicebus.QueueClient;
import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.IMessageSender;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
Expand All @@ -10,7 +11,7 @@ public class SendSample {

private static final String ENVIRONMENT_VARIABLE_NAME = "azure-service-bus-java/connectionstring";
private static final int NUMBER_OF_MESSAGES = 1;
private static IMessageSender sender;
private static QueueClient queueClient;

public static void main(String[] args) throws Exception {
System.out.println("Begining send sample.");
Expand All @@ -19,20 +20,20 @@ public static void main(String[] args) throws Exception {

if (envVar.isEmpty()) {
throw new Exception("Could not read environment variable: " + ENVIRONMENT_VARIABLE_NAME);
}
}

ConnectionStringBuilder csb = new ConnectionStringBuilder(envVar);
sender = ClientFactory.createMessageSenderFromConnectionStringBuilder(csb);
QueueClient queueClient = new QueueClient(envVar, ReceiveMode.PeekLock);

sendMessages(NUMBER_OF_MESSAGES);
queueClient.close();
System.out.println("Send sample completed.");
}

private static void sendMessages(int numMessages) throws InterruptedException, ServiceBusException {
for(int i = 0; i < numMessages; i ++) {
String messageBody = "MessageNumber: " + i;
Message message = new Message(messageBody.getBytes());
sender.send(message);
queueClient.send(message);
System.out.println("Sending message " + i);
}
}
Expand Down

0 comments on commit 9f9d4be

Please sign in to comment.