How can I make publish and subscribe in one file? - rabbitmq

I want to make easy application with sending messages (rabbitmq). When run program I want to write messages and receive it (in same console). So how can I write a method "subcribe" which will be running all the time(after starting program) and waiting to receive message? and at same time of course i should be able to write a message

Create a supervised app (mix new my_app --sup ).
You implement two GenServer, one for receiving (MyApp.Reader) and the other one for sending (MyApp.Writer). You add both GenServer as worker in your supervisor spec.
I guess your RabbitMQ adapter (never used it with elixir so you'll have to double check) will also be supervised. Name it, and pass its name to both GenServer init method (via the args you pass when declaring your workers for example)
You can also implement your API for the writer directly in MyApp. Something like:
def send(message) do
GenServer.cast(:name_of_writer, $message)
end
You'll then be able to do: MyApp.send("some message")
Your receiver worker, would on its side, receive all messages, and print them to the CLi, either using inspect or the Logger module
Have a look at the GenServer docs, there is some code example that will help you.

Related

Messages Patterns - RabbitMQ/NestJS

I'm trying to integrate to a project done using NestJS, a simple api where you can publish messages with a name (or pattern) and send it to that system that has implemented a handler that matches the name.
The system I'm building is really small, it wouldn't make much of a sense using NestJS for that.
The problem I'm having is the following:
I'm creating a simple api that triggers the publish of the message onto a queue.
The consumer is on a system using NestJS.
I can't figure out how to give that messages a pattern that is recognized by that system.
For example:
Let's say I want to publish a message that has a name of "CreateRecord" with a payload to be processed from the other system that has a handler with the same name with an implementation.
Using amqplib how do I give messages a name or pattern?
You can provide a pattern in the content object when you are publishing a message.
publish(
'my_exchange',
'routing_key',
{ pattern: 'CreateRecord', data: 'Record' },
);

how to test a hotfolder that use channels in spring-integration?

in spring-integretion project,i have a hot folder that detects when a csv is placed in a folder and do some extra stuff, i have a inbound-channel-adapter connected with a channel.
inbound-channel-adapter -> channel.
detects when a csv its placed receive the mns its connected with
a service-activator
what i what to do its test only that the channel its receiving the mns when a file its created
i am using this tutorial
https://www.javacodegeeks.com/2013/05/spring-integration-file-polling-and-tests.html
its very useful, but i can create the context
Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener#57a4d5ee] to prepare test instance [proyect.integration.hotFolderTest#5af5def9]
java.lang.IllegalStateException: Failed to load ApplicationContext
You need to show more stack trace on the matter, because right now it isn't clear what is your real issue.
Also would be great to share some code what you have so far and what you would like to do in the test case.
To verify a message presence in the channel you can configure a ChannelInterceptor and implement its preSend().
However we also suggest something like #SpringIntegrationTest with the MockIntegration features. That way you can replace your real service activator with some MockIntegration.mockMessageHandler() and perform verification on it.
You would need to configure a noAutoStartup on the mentioned annotation do not poll directory until you prepare your mock and call a this.mockIntegrationContext.substituteMessageHandlerFor().
See more info in the Reference Manual: https://docs.spring.io/spring-integration/docs/current/reference/html/testing.html#test-context

MarshalByRefObject causing C++ program to hang

I have a client program that uses a MarshalByRefObject to get a variable from a remote server. Sometimes the program hoses up on the remote server and when I try to get that variable my client program simply hangs. Is there a way to time out the call on this variable?
MyClass^ refObject = (MyClass^)System::Activator::GetObject(MyClass::typeid, url);
THEVARIABLE objectVariable = refObject->theVariable;
The only way I see is to implement an IMessageFilter (COM). In some cases it is possible to detect that there is an out of process call from the current STA to another. But AFAIK this is only done when an input message (keyboard/mouse) arrives.
With a message filter you can show something like "waiting for external com call...". Also in this case you may abort the external call.
See CoRegisterMessageFilter, and IMessageFilter

Using Twilio StatusCallback when a call on queue hangs up

I'm implementing a Twilio-powered Call Center and I'm currently using Enqueue to hold calls until an operator can answer them. I'd like to use StatusCallback to warn operators that a call that was on hold is now "completed" (caller terminated the call) but StatusCallback seems only to work with a "Client" verb. I'd want to use it with along with "Enqueue" or a "Gather" in the WaitURL.
Am I missing something?
I found that setting the StatusCallback endpoint in the Application configuration in the Twilio Dashboard globally solves this.

Is it possible to write a plugin for Glimpse's existing SQL tab?

Is it possible to write a plugin for Glimpse's existing SQL tab?
I'm trying to log my SQL queries and the currently available extensions don't support our in-house SQL libary. I have written a custom plugin which logs what I want, but it has limited functionality and it doesn't integrate with the existing SQL tab.
Currently, I'm logging to my custom plugin using a single helper method inside my DAL's base class. This function looks takes the SqlCommand and Duration in order to show data on my custom tab:
// simplified example:
Stopwatch sw = Stopwatch.StartNew();
sqlCommand.Connection = sqlConnection;
sqlConnection.Open();
object result = sqlCommand.ExecuteScalar();
sqlConnection.Close();
sw.Stop();
long duration = sw.ElapsedMilliseconds;
LogSqlActivity(sqlCommand, null, duration);
This works well on my 'custom' tab but unfortunately means I don't get metrics shown on Glimpse's HUD:
Is there a way I can provide Glimpse directly with the info it needs (in terms of method names, and parameters) so it displays natively on the SQL tab?
The following advise is based on the fact that you can't use DbProviderFactory and you can't use a proxied SqlCommand, etc.
The data that appears in the "out-of-the-box" SQL tab is based on messages of given types been published through our internal Message Broker (see below on information on this). Because of the above limitations in your case, to get things lighting up correctly (i.e. your data showing up in HUD and the SQL tab), you will need to simulate the work that we do under the covers when we publish these messages. This shouldn't be that difficult and once done, should just work moving forward.
If you have a look at the various proxies we have here you will be above to see what messages we publish in what circumstances. Here are some highlights:
DbCommand
Log command start - here
Log command error - here
Log command end - here
DbConnection:
Log connection open - here
Log connection closed - here
DbTransaction
Log Started - here
Log committed - here
Log rollback - here
Other
Command row count here - Glimpses calculates this at the DbDataReader level but you could do it elsewhere as well
Now that you have an idea of what messages we are expecting and how we generate them, as long as you pass in the right data when you publish those messages, everything should just light up - if you are interested here is the code that looks for the messages that you will be publishing.
Message Broker: If you at the GlimpseConfiguration here you will see how to access the Broker. This can be done statically if needed (as we do here). From here you can publish the messages you need.
Helpers: For generating some of the above messages, you can use the helpers inside the Support class here. I would have shifted all the code for publishing the actual messages to this class, but I didn't think there would be too many people doing what you are doing.
Update 1
Starting point: With the above approach you shouldn't need to write your own plugin. You should just be able to access the broker GlimpseConfiguration.GetConfiguredMessageBroker() (make sure you check if its null, which it is if Glimpse is turned off, etc) and publish your messages.
I would imagine that you would put the inspection that leverages the broker and published the messages, where ever you have knowledge of the information that needs to be collected (i.e. inside your custom lib). Normally this would require references inside your lib to glimpse (which you may not want), so to protect against this, from your lib, you would call a proxy (which could be another VS proj) that has the glimpse dependency. Hence your ado lib only has references to your own code.
To get your toes wet, try just publishing a couple of fake connection and command messages. Assuming the broker you get from GlimpseConfiguration.GetConfiguredMessageBroker() isn't null, these should just show up. Then you can work towards getting real data into it from your lib.
Update 2
Obsolete Broker Access
Its marked as obsolete because its going to change in v2. You will still be able to do what you need to do, but the way of accessing the broker has changed. For what you currently need to do this is ok.
Sometimes null
As you have found this is really dependent on where in the page lifecycle you are currently at. To get around this, I would probably change my original recommendation a little.
In the code where you are currently creating messages and pushing them to the message bus, try putting them into HttpContext.Current.Items. If you haven't used it before, this is a store which asp.net provides out of the box which lasts the lifetime of a given request. You could have a list that you put in there, still create the message objects that you are doing, but put them into that list instead of pushing them through the broker.
Then, create a HttpModule (its really simple to do) which taps into the PostLogRequest event. Within this handler, you would pull the list out of the context, iterate through it and push the message into the message broker (accessing the same way you have been).