gpt4 book ai didi

java - 如何等待进程完成 Java AWS Lambda

转载 作者:行者123 更新时间:2023-11-30 02:00:56 25 4
gpt4 key购买 nike

我想等到我的流程完成后再返回我的语音响应,否则它似乎会切断我的流程,因此不完成它,我实际上相信它可能会卡住该流程,但这不是我的愿望。

我该如何等待?

@Override
public SpeechletResponse onIntent(SpeechletRequestEnvelope<IntentRequest> requestEnvelope) {
IntentRequest request = requestEnvelope.getRequest();

Intent intent = request.getIntent();
String intentName = (intent != null) ? intent.getName() : null;

if (intentName == null) return null;

switch (intentName) {
case IntentTitle.NEW_TICKET:

switch (request.getDialogState()) {
case STARTED:
return Response.getDialogueResponse(intent, true);
case IN_PROGRESS:
return Response.getDialogueResponse(intent, false);
case COMPLETED:
String numberString = intent.getSlot(SlotTitle.ID).getValue();
if (!NumberUtils.isCreatable(numberString)) return Response.ERROR;

Member member = Info.GUILD.getMemberById(numberString);
User sender = UserDB.getUser(member);

CommissionTicket commissionTicket = new CommissionTicket(sender);
commissionTicket.create();

//wait until processes finish before continuing

return Response.NEW_TICKED_CREATED;
}
}

return null;
}

更新:查看了 CloudWatch 日志,嗯,几乎我预期发生的情况正在发生...查看这些日志的时间(我运行了 3 次不同的时间,因此记录了 3 个不同的时间,但除了相同的 lambda session )

public void create() {
System.out.println("log2");
GuildController guildController = Info.GUILD.getController();

guildController.createTextChannel(ticketType.name().toLowerCase() + "-" + creator.getName() + "-" + id.value()).queue(channel -> {
System.out.println("log3");
ChannelManager channelManager = channel.getManager();
GuildManager guildManager = channelManager.getGuild().getManager();
List<Member> members = guildManager.getGuild().getMembers();
List<Member> admins = new ArrayList<>();

for (Member member : members) {
for (Role role : member.getRoles()) {
if (!role.getName().equalsIgnoreCase(Info.ADMIN_STRING)) continue;
admins.add(member);
}
}
System.out.println("log4");
for (Member member : members) {
if (member.equals(creator.getMember())) continue;
channel.createPermissionOverride(member).setDeny(Permission.MESSAGE_READ).queue();
}
System.out.println("log5");

for (Member admin : admins) {
if (admin.equals(creator.getMember())) continue;
channel.createPermissionOverride(admin).setAllow(Permission.MESSAGE_READ).queue();
}
System.out.println("log6");

BotMessage botMessage = new BotMessage();
botMessage
.setTitle("New Ticket! User: " + creator.getName())
.setColour(Color.CYAN)
.setDescription("Please enter your full request here! \n" +
"Make sure to let us know whether you are looking for a quote/timeframe,\n" +
"or have a budget in mind, and we will work around you!\n\n" +
"A sales representative will be with you as soon as possible!")
.send((TextChannel) channel);
System.out.println("log7");

this.textChannel = (TextChannel) channel;

TicketDB.addTicket(this);
System.out.println("log8");
}
);

日志:

https://gyazo.com/0ad2baa8d1438be8364dd1112159c4f4 https://gyazo.com/e197f33335046afe3c9f8f1ace267d30

更新实现了 Future 类,有效,但仍然有一些问题。它现在完全创建了票证,这很好,但是,当我再次发送相同的调用时,由于某种原因,它在创建下一个票证之前在同一 channel 中发送一条消息。

所以,要模拟...

通过AWS Lambda调用函数

完全创建票证

再次通过AWS Lambda调用函数

在之前的门票 channel 中发送一两条消息

完全创建新票

https://gyazo.com/dc6e4391f4964f41a73f1c3be92190f9

@Override
public SpeechletResponse onIntent(SpeechletRequestEnvelope<IntentRequest> requestEnvelope) {
IntentRequest request = requestEnvelope.getRequest();

Intent intent = request.getIntent();
String intentName = (intent != null) ? intent.getName() : null;

if (intentName == null) return null;

switch (intentName) {
case IntentTitle.NEW_TICKET:

switch (request.getDialogState()) {
case STARTED:
return Response.getDialogueResponse(intent, true);
case IN_PROGRESS:
return Response.getDialogueResponse(intent, false);
case COMPLETED:
String numberString = intent.getSlot(SlotTitle.ID).getValue();
if (!NumberUtils.isCreatable(numberString)) return Response.ERROR;

Member member = Info.GUILD.getMemberById(numberString);
User sender = UserDB.getUser(member);

System.out.println("log1");

Future<Ticket> commissionTicket = new CommissionTicket(sender).create();
try {
commissionTicket.get(10000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
e.printStackTrace();
}

//wait until processes finish before continuing

return Response.NEW_TICKED_CREATED;
}
}

return null;
}

在 Ticket 类中:

public Future<Ticket> create() {
System.out.println("log2");
GuildController guildController = Info.GUILD.getController();
RequestFuture<Channel> channelRequestFuture = guildController.createTextChannel(ticketType.name().toLowerCase() + "-" + creator.getName() + "-" + id.value()).submit();
try {
Channel channel = channelRequestFuture.get(10000, TimeUnit.MILLISECONDS);
System.out.println("log3");
ChannelManager channelManager = channel.getManager();
GuildManager guildManager = channelManager.getGuild().getManager();
List<Member> members = guildManager.getGuild().getMembers();
List<Member> admins = new ArrayList<>();

for (Member member : members) {
for (Role role : member.getRoles()) {
if (!role.getName().equalsIgnoreCase(Info.ADMIN_STRING)) continue;
admins.add(member);
}
}
System.out.println("log4");
for (Member member : members) {
if (member.equals(creator.getMember())) continue;
channel.createPermissionOverride(member).setDeny(Permission.MESSAGE_READ).queue();
}
System.out.println("log5");

for (Member admin : admins) {
if (admin.equals(creator.getMember())) continue;
channel.createPermissionOverride(admin).setAllow(Permission.MESSAGE_READ).queue();
}
System.out.println("log6");

BotMessage botMessage = new BotMessage();
botMessage
.setTitle("New Ticket! User: " + creator.getName())
.setColour(Color.CYAN)
.setDescription("Please enter your full request here! \n" +
"Make sure to let us know whether you are looking for a quote/timeframe,\n" +
"or have a budget in mind, and we will work around you!\n\n" +
"A sales representative will be with you as soon as possible!")
.send((TextChannel) channel);
System.out.println("log7");

this.textChannel = (TextChannel) channel;

TicketDB.addTicket(this);
System.out.println("log8");
Future<Ticket> future = ConcurrentUtils.constantFuture(this);
return future;
} catch (Exception e) {
e.printStackTrace();
}

if (!userIsInTicket(creator)) users.add(creator);
return null;

}

最佳答案

我不确定里面发生了什么commissionTicket.create(); (我假设这是您的代码,而不是某个库的一部分,并且它以某种方式异步运行),但一种解决方案是让该方法返回 Future<>对象,然后等待它完成。大致如下:

Future<CommissionTicket> commissionTicketFuture = CommissionTicket.create(sender);
commissionTicketFuture.get(SOME_TIMEOUT, TimeUnit.MILLIS);
return Response.NEW_TICKET_CREATED;

关于java - 如何等待进程完成 Java AWS Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52865547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com