com.microsoft.eventhubs.client.EventHubSender Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of eventhubs-client Show documentation
Show all versions of eventhubs-client Show documentation
A generic EventHubs client library for JVM
/*******************************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.microsoft.eventhubs.client;
import java.util.concurrent.TimeoutException;
import org.apache.qpid.amqp_1_0.client.LinkDetachedException;
import org.apache.qpid.amqp_1_0.client.Message;
import org.apache.qpid.amqp_1_0.client.Sender;
import org.apache.qpid.amqp_1_0.client.Session;
import org.apache.qpid.amqp_1_0.type.Binary;
import org.apache.qpid.amqp_1_0.type.messaging.Data;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EventHubSender {
private static final Logger logger = LoggerFactory.getLogger(EventHubSender.class);
private final Session session;
private final String entityPath;
private final String partitionId;
private final String destinationAddress;
private Sender sender;
public EventHubSender(Session session, String entityPath, String partitionId) {
this.session = session;
this.entityPath = entityPath;
this.partitionId = partitionId;
this.destinationAddress = getDestinationAddress();
}
public void send(byte[] data) throws EventHubException {
try {
if (sender == null) {
ensureSenderCreated();
}
Binary bin = new Binary(data);
Message message = new Message(new Data(bin));
sender.send(message);
} catch (LinkDetachedException e) {
logger.error(e.getMessage());
EventHubException eventHubException = new EventHubException("Sender has been closed");
throw eventHubException;
} catch (TimeoutException e) {
logger.error(e.getMessage());
EventHubException eventHubException = new EventHubException("Timed out while waiting to get credit to send");
throw eventHubException;
} catch (Exception e) {
logger.error(e.getMessage());
}
}
public void send(String data) throws EventHubException {
//For interop with other language, convert string to bytes
send(data.getBytes());
}
public void close() {
try {
sender.close();
} catch (Sender.SenderClosingException e) {
logger.error("Closing a sender encountered error: " + e.getMessage());
}
}
private String getDestinationAddress() {
if (partitionId == null || partitionId.equals("")) {
return entityPath;
} else {
return String.format(Constants.DestinationAddressFormatString, entityPath, partitionId);
}
}
private synchronized void ensureSenderCreated() throws Exception {
if (sender == null) {
sender = session.createSender(destinationAddress);
}
}
}