Spray.IO Stop http server with Http.Unbind - spray

I have an app built on spray 1.3.3 and I'm trying to stop http server by sending message.
Official documentation says:
Stopping. To explicitly stop the server, send an Http.Unbind command to
the HttpListener instance (the ActorRef for this instance is available
as the sender of the Http.Bound confirmation event from when the
server was started).
Here is how i the start server:
(IO(Http) ? Http.Bind(httpListener, interface = iface, port = port)).map {
case m: Http.Bound => println(s"Success $iface:$port")
case fail : Http.CommandFailed => println(s"Bad try :(")
}
I tried to send message Http.Unbind to httpListener but with no success. Seems to be it's not that actor
May be i need to extract somehow sender ref of Http.Bound message? But how?
Here is my httpListener's head:
class MyHttpListener extends HttpServiceActor {
import context.dispatcher
def receive = runRoute(
path("shutdown") {
get {
actors.httpListener ! Http.Unbind
complete("Stopping server")
}
}
Anyway, i just want to send http request to /shutdown and have my application be down

The sender of the Bound message should be the Spray listener, so when receiving that message you can capture it.
You did not post your full actor so this is a little different but I hope this helps.
class MyStarter extends Actor {
// this will be the listener
var myListener: ActorRef = _
override def preStart(): Unit = {
implicit val system = context.system
IO(Http) ! Http.Bind(...)
}
override def receive: Receive = {
case msg: Bound =>
// when you receive the Bound message it was sent by the listener
myListener = sender()
...
case ... => ...
}
}

Related

Unable to get Twilio sms status callbacks when sending proactive message

I'm trying to track the sms delivery status of the messages I send using the bot framework. I'm using Twilio, and sending proactive messages. Right now I'm trying to do so with twilio status callbacks
This is similar to this question, I tried that approach but I couldn't get it to work. I've added my url on the TwiML app and it is not firing. I have double and triple checked, and I suspect this url is somehow ignored or not going through with my current set up. I don't get any callbacks on the proactive message nor on the replies the bot sends to the user. However the flow works fine and I can reply and get proper responses from the bot. Edit: calling this "approach 1"
approach 2: I've also tried this doing some light modifications on Twilio adapter, to be able to add my callback just before create message. (I changed it so it uses a customized client wrapper that adds my callback url when creating the twilio resource) This does work, partially: when I reply a message from my bot, I get the status callbacks. But as the proactive message is sent using the default adapter, I don't get a callback on the initial message.
approach 3: Finally, I also tried using the TwilioAdapter when sending the proactive message but for some reason as soon as I send an activity, the TurnContext is disposed, so I can't save the state or do any subsequent actions. This leads me to believe twilio adapter is not intended to be used this way (can't be used on proactive messages), but I'm willing to explore this path if necessary.
Here is the modified Twilio Adapter:
public class TwilioAdapterWithErrorHandler : TwilioAdapter
{
private const string TwilioNumberKey = "TwilioNumber";
private const string TwilioAccountSidKey = "TwilioAccountSid";
private const string TwilioAuthTokenKey = "TwilioAuthToken";
private const string TwilioValidationUrlKey = "TwilioValidationUrl";
public TwilioAdapterWithErrorHandler(IConfiguration configuration, ILogger<TwilioAdapter> logger, TwilioAdapterOptions adapterOptions = null)
: base(
new TwilioClientWrapperWithCallback(new TwilioClientWrapperOptions(configuration[TwilioNumberKey], configuration[TwilioAccountSidKey], configuration[TwilioAuthTokenKey], new Uri(configuration[TwilioValidationUrlKey]))), adapterOptions, logger)
{
OnTurnError = async (turnContext, exception) =>
{
// Log any leaked exception from the application.
logger.LogError(exception, $"[OnTurnError] unhandled error : {exception.Message}");
Task[] tasks = {
// Send a message to the user
turnContext.SendActivityAsync("We're sorry but this bot encountered an error when processing your answer."),
// Send a trace activity, which will be displayed in the Bot Framework Emulator
turnContext.TraceActivityAsync("OnTurnError Trace", exception.Message, "https://www.botframework.com/schemas/error", "TurnError")
};
Task all = Task.WhenAll(tasks); //task with the long running tasks
await Task.WhenAny(all, Task.Delay(5000)); //wait with a timeout
};
}
}
Modified client Wrapper:
public class TwilioClientWrapperWithCallback : TwilioClientWrapper
{
public TwilioClientWrapperWithCallback(TwilioClientWrapperOptions options) : base(options) { }
public async override Task<string> SendMessageAsync(TwilioMessageOptions messageOptions, CancellationToken cancellationToken)
{
var createMessageOptions = new CreateMessageOptions(messageOptions.To)
{
ApplicationSid = messageOptions.ApplicationSid,
MediaUrl = messageOptions.MediaUrl,
Body = messageOptions.Body,
From = messageOptions.From,
};
createMessageOptions.StatusCallback = new System.Uri("https://myApp.ngrok.io/api/TwilioSms/SmsStatusUpdated");
var messageResource = await MessageResource.CreateAsync(createMessageOptions).ConfigureAwait(false);
return messageResource.Sid;
}
}
Finally, here's my summarized code that sends the proactive message:
[HttpPost("StartConversationWithSuperBill/{superBillId:long}")]
[HttpPost("StartConversationWithSuperBill/{superBillId:long}/Campaign/{campaignId:int}")]
public async Task<IActionResult> StartConversation(long superBillId, int? campaignId)
{
ConversationReference conversationReference = this.GetConversationReference("+17545517768");
//Start a new conversation.
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, async (turnContext, token) =>
{
await turnContext.SendActivityAsync("proactive message 1");
//this code was edited for brevity. Here I would start a new dialog that would cascade into another, but the end result is the same, as soon as a message is sent, the turn context is disposed.
await turnContext.SendActivityAsync("proactive message 2"); //throws ObjectDisposedException
}, default(CancellationToken));
var result = new { status = "Initialized fine!" };
return new JsonResult(result);
}
private ConversationReference GetConversationReference(string targetNumber)
{
string fromNumber = "+18632704234";
return new ConversationReference
{
User = new ChannelAccount { Id = targetNumber, Role = "user" },
Bot = new ChannelAccount { Id = fromNumber, Role = "bot" },
Conversation = new ConversationAccount { Id = targetNumber },
//ChannelId = "sms",
ChannelId = "twilio-sms", //appparently when using twilio adapter we need to set this. if using TwiML app and not using Twilio Adapter, use the above. Otherwise the frameworks interprets answers from SMS as new conversations instead.
ServiceUrl = "https://sms.botframework.com/",
};
}
(I can see that I could just call create conversation reference and do two callbacks, one for each message, but in my actual code I'm creating a dialog that sends one message and then invokes another dialog that starts another message)
Edit 2:
Some clarifications:
On approach 2, I'm using two adapters, as suggested by code sample and documentation on using twilio adapter. The controller that starts the proactive message uses an instance of a default adapter (similar to this one), and TwilioController (the one that gets the twilio incoming messages) uses TwilioAdapterWithErrorHandler.
On approach 3, I excluded the default adapter, and both controllers use TwilioAdapterWithErrorHandler.
Edit 3:
Here's a small repo with the issue.
I found a fix for this problem, around approach 3, by changing the overload I use for ContinueConversation. Replace this :
//Start a new conversation.
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, async (turnContext, token) =>
With this:
//Start a new conversation.
var twilioAdapter = (TwilioAdapterWithErrorHandler)_adapter;
await twilioAdapter.ContinueConversationAsync(_appId, conversationReference, async (context, token) =>
This way, the context is not disposed, an I can use the twilio adapter for the proactive message and have status callbacks on all messages.

Solace Client Acknowledgement Replay

I'm putting together a Solace Point-to-point solution in C#.
In my subscriber/listener, I am using ClientAck mode to ensure messages are successfully processed before being removed from the queue.
My question (probably due to my limited experience in messaging) is regarding failed messages, e.g. if I cannot process the message and hence not send the Ack, how is the message replayed?
An example of what I have is as follows:
using (ISession session = context.CreateSession(sessionProperties, null, null))
{
ReturnCode returnCode = session.Connect();
if (returnCode == ReturnCode.SOLCLIENT_OK)
{
var endpointProps = new EndpointProperties()
{
Permission = EndpointProperties.EndpointPermission.Consume,
AccessType = EndpointProperties.EndpointAccessType.Exclusive
};
using (IQueue queue = ContextFactory.Instance.CreateQueue(queueName))
{
session.Provision(queue, endpointProps,
ProvisionFlag.IgnoreErrorIfEndpointAlreadyExists | ProvisionFlag.WaitForConfirm, null);
_flow = session.CreateFlow(new FlowProperties { AckMode = MessageAckMode.ClientAck }, queue, null, HandleMessageEvent, HandleFlowEvent);
_flow.Start();
do { WaitEventWaitHandle.WaitOne(); } while (!cancellationToken.IsCancellationRequested);
};
return Task.CompletedTask;
}
else
{
throw new Exception($"Connection failed, return code: {returnCode}");
}
}
and then handling incoming messages
void HandleMessageEvent(object sender, MessageEventArgs args)
{
using (IMessage message = args.Message)
{
try
{
_handler(message.ApplicationMessageType, message.BinaryAttachment);
_flow.Ack(message.ADMessageId);
}
finally
{
WaitEventWaitHandle.Set();
}
}
}
So, if I don't Ack, message remains on queue as expected (and required), however, how (best-practice) can I re-process it without manual intervention?
After a message has been delivered to a consumer from a Solace PubSub+ queue, the message will only be resent if the client unbinds before sending an acknowledgement back. The exception to this is specific to JMS clients with the session.recover() action.
If a message needs to be re-delivered to a C# application after it has already been sent but not acknowledged, the client will need to unbind and rebind to the queue. Note that if there are other clients also bound to the queue, the message may be re-sent to those clients before your client rebinds.

django-channels parse json/data sent via sockets

I have this javascript code that send data to channels
// Note that the path doesn't matter for routing; any WebSocket
// connection gets bumped over to WebSocket consumers
socket = new WebSocket("ws://" + window.location.host + "/chat/");
socket.onmessage = function(e) {
alert(e.data);
}
socket.onopen = function() {
socket.send({"test":"data"});
}
// Call onopen directly if socket is already open
if (socket.readyState == WebSocket.OPEN) socket.onopen();
I'm curios how from message I can get the json {"test":"data"}
here's the view
# Connected to websocket.connect
#channel_session
def ws_connect(message, key):
# Accept connection
message.reply_channel.send({"accept": True})
You implemented the connection callback, but did not implement what should happen when a message arrives the server endpoint. Add add message receive function:
def on_receive(message):
print('test received: {}'.format(message.content['test']))
Register the function in routing.py:
channel_routing = [
route("websocket.connect", ws_connect),
route("websocket.receive", on_receive),
]
The JSON message you send will be stored in message.content which is basically just a python dict.

grails jms onMessage receives message text?? not Message

I have this in my listener service (following doc):
#Queue(name='queue.web.dev')
def onMessage(Message msg) {
println "DEBUG msgCorrelationID :"+msg.getJMSCorrelationID()
}
but on receiving message, I got error:
"No signature of method: java.lang.String.getJMSCorrelationID()"
If I try adding Message to onMessage args like:
def onMessage(Message msg)
I got error:
java.lang.NoSuchMethodException: MessageListenerService$$EnhancerBySpringCGLIB$$4bfa7a63.onMessage(java.lang.String)
Looks to me that onMessage is getting String message text. Is that true? How can I get whole jms.Message then?
Don't know if this is still open but you have to take the object out of the message yourself ..
def messageContents = ((ObjectMessage) message).getObject()
Is that what your looking for ?
Remove the default message converter
jms {
containers {
standard {
messageConverter = null
}
}
}
Check grails jms docs here

Grails Atmosphere Plugin broadcast to different clients

I'm trying to extend the Groovy Mag Atmosphere Example (https://github.com/rbramley/GroovyMagJMS) to broadcast to different clients. (Like in Broadcasting to a subset of subscribers in Atmosphere)
A client connects with url http://localhost:8080/GrailsTest/atmosphere/messages/?id=1. An id will be passed to the server. The new added lookupBroadcaster Method creates a new Broadcaster Object with the id. When I wanna broadcast a message, the client does not receive the result.
Can somebody help me and maybe try it out?
I'm added the atmosphere 0.8.2 library to BuildConfig.groovy to use mappings like '/atmosphere/messages/*'.
dependencies {
runtime 'org.atmosphere:atmosphere-runtime:0.8.2'
}
class AtmosphereService {
static transactional = false
static atmosphere = [mapping: '/atmosphere/messages/*']
static exposes = ['jms']
#Subscriber(topic='msgevent')
def onEvent(msg) {
println 'onevent'
def payload = msg
if(msg instanceof Map) {
// convert map messages to JSON
payload = msg.encodeAsJSON()
}
Broadcaster b = lookupBroadcaster(msg["id"], false);
b.broadcast(payload)
return null
}
Broadcaster lookupBroadcaster(String id, Boolean createBroadcast) {
return BroadcasterFactory.getDefault().lookup(id, createBroadcast)
}
def onRequest = { event ->
def req = event.request
def id = req.getParameter("id")
Broadcaster b = lookupBroadcaster(id, true);
event.setBroadcaster(b);
b.addAtmosphereResource(event)
event.suspend()
}
def onStateChange = { event ->
if (event.message) {
log.info "onStateChange, message: ${event.message}"
if (event.isSuspended()) {
event.resource.response.writer.with {
write "<script>parent.callback('${event.message}');</script>"
flush()
}
event.resume()
}
}
}
}
That should work based on that code snippet. Is the onStateChange() method invoked when you broadcast? Since you are resuming, the first broadcast will works but after that the AtmosphereResource will be removed from its associated Broadcaster, hence no more update.

Resources