Я новичок в приложении-функции Azure, у меня есть требование, когда мне нужно иметь триггер очереди, на основе которого мне нужно вставить запись в таблицу хранения.
@FunctionName("echo")
public void echo( @HttpTrigger(name = "req", methods = { "post" }, authLevel = AuthorizationLevel.ANONYMOUS,route="items") String in,
@TableOutput(name = "$return", tableName = "people", partitionKey="lastName" ,rowKey = "firstName" , connection = "AzureWebJobsStorage") OutputBinding<CustomerEntity> obj,
ExecutionContext context ) {
context.getLogger().info( "Hello, " + in );
ObjectMapper objectMapper = new ObjectMapper();
MyItem myItem = null;
try {
myItem = objectMapper.readValue(in, MyItem.class);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
context.getLogger().info( "Hello, " + in + " and " + myItem.getNameOfValue() + ".");
CustomerEntity customerEntity = new CustomerEntity(myItem.getNameOfValue(), myItem.getId());
customerEntity.setEmail("Walter@contoso.com");
customerEntity.setPhoneNumber("425-555-0101");
obj.setValue(customerEntity);
}
Класс CustomerEntity открытый класс CustomerEntityextends TableServiceEntity {
public CustomerEntity(String lastName, String firstName) {
this.partitionKey = lastName;
this.rowKey = firstName;
}
public CustomerEntity() { }
String email;
String phoneNumber;
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhoneNumber() {
return this.phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
}
В этом случае функция вызывается успешно, но она не печатает журналы и не помещает данные в таблицы хранения.
Я использую локальное хранилищедля этого.
Я проверил, что приложение работает автономно с клиентом Table, но не работает в приложениях функций.
Спасибо, Балки
Так что после большого поиска яполучил рабочий код
public class QueueTriggerJava {
/**
* This function will be invoked when a new message is received at the specified path. The message contents are provided as input to this function.
*/
@FunctionName("QueueTriggerJava")
public void run(
@QueueTrigger(name = "message", queueName = "enqueue", connection = "AzureWebJobsStorage") String message,
@TableOutput(connection="AzureWebJobsStorage",name="table",tableName="people",partitionKey="orders",rowKey="1") OutputBinding<People> out,
final ExecutionContext context
) throws Exception {
context.getLogger().info("Java Queue trigger function processed a message: " + message);
People people = new People(String.valueOf(RandomStringUtils.randomAlphanumeric(10, 20)));
ObjectMapper objectMapper = new ObjectMapper();
People peoplew = objectMapper.readValue(message, People.class);
people.setId(peoplew.getId());
people.setNameOfValue(peoplew.getNameOfValue());
out.setValue(people);
context.getLogger().info("Java Queue trigger function processed a message: " + message);
}
}
Я сталкиваюсь с проблемой, что всякий раз, когда я помещаю сообщение в очередь, он получает тот же rowKey, наиболее вероятно bcoz статического значения rowkey.
public class People{
private String PartitionKey;
private String RowKey;
private String nameOfValue;
private String id;
public People(){
}
public People (final String rowKey){
this.PartitionKey = "Orders";
this.RowKey = rowKey;
}
/**
* @param id the id to set
*/
public void setId(String id) {
this.id = id;
}
/**
* @return the id
*/
public String getId() {
return id;
}
/**
* @param nameOfValue the nameOfValue to set
*/
public void setNameOfValue(String nameOfValue) {
this.nameOfValue = nameOfValue;
}
/**
* @return the nameOfValue
*/
public String getNameOfValue() {
return nameOfValue;
}
}