Asterisk-java. Queues action - java

Help me please with the question:
I need to get information about the queue (the best option would be for a particular queue and all queues). I use the asterisk-Java 2.0.3 library. For information about a particular queue, I can send the action - QueueSummaryAction. To get all the queues I can use the AsteriskServer object and get the information using getQueues(). However, this information is slightly incomplete.
In addition, I connect to the server Asterisk via telnet. And after doing the appropriate action (action: queues), I get a list of queues with pretty good information (see image).
I would like to somehow get this with the help of AMI and the implementation presented in this library. Can there be any analogy? For example, I'm interested in the number of calls, the number of responses, deductions (in image it's W:0, C:5, A:24, SL:100.0%, within 60s). But, unfortunately, I did not find a way to get such information. Part of the presented in the image, I can get, but I would like more (regarding the queue itself, not the queue members).
About my implementation.
I create a new DefaultAsteriskServer. Next, I add my listeners (I will not describe them here).
public void build(String hostname, int port, String username, String password)
{
asteriskServer = new DefaultAsteriskServer(hostname, port, username, password);
asteriskServer.initialize();
LOGGER.info("Asterisk server initialized");
}
public void addEventListener(ManagerEventListener eventListener)
{
asteriskServer.getManagerConnection().addEventListener(eventListener);
}
Actually, that's all.
Thank you all in advance for your help.

You should issue manager action "COMMAND"
In parameters you can put same request you show on your screen

Related

How can I send a message directly into a dead letter queue in azure service bus (java)?

I have a servicebusqueuetrigger function that takes a string off the service bus and calls another api, if that api call does not return a 200, I want to move that string as a meesage into a dead letter queue. How can I do this?
So far I have this...
public class myEventsConsumerFunction {
#FunctionName("myEventTrigger")
public void myEventTrigger(
#ServiceBusQueueTrigger(name = "message", queueName = "%QUEUE_NAME%", connection = "ServiceBusConnection") String message,
final ExecutionContext context) throws MSPException {
int responseCode = callApi(message);
if(responseCode!=200){
String ConnectionString = "connectionstring"
QueueClient sendClient = new QueueClient(new ConnectionStringBuilder(ConnectionString, queueName), ReceiveMode.PEEKLOCK);
//
//how do i send my String message to dead letter queue here?
//
}
I see a bunch of methods in queue client called deadletter, but they take a UUID as a parameter (I am not sure what that is), and not a message String.
The key to the problem is that the Java-based azure sdk package of offcial didn't not write the MessageReceiver class. (As far as I know, the official only implements the IMessageReceiver interface of C#) If it is C#, you can use it directly:
https://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.servicebus.core.messagereceiver?view=azure-dotnet
But for java, The official has not yet implemented this, only a interface:
https://learn.microsoft.com/en-us/java/api/com.microsoft.azure.servicebus.imessagereceiver?view=azure-java-stable
I see a bunch of methods in queue client called deadletter, but they
take a UUID as a parameter (I am not sure what that is), and not a
message String.
I know this, but it is not based on messages, if you create an object of queueclient and use the method, it will tells you no receiver:
In summary, I think this is an unfinished feature, and maybe it can be updated in a future sdk version. But not now, unless you read the underlying code and implement the IMessageReceiver interface yourself, otherwise it will be impossible.
I encountered the same issue, the github issue is still open.

How could I identify multiple commands for network application (Java)

I want to build a client-server-application for some practice. I started off with a simple chat which was not so hard to do. I'm also able to identify different commands by just simply split a String, e.g. "command:msg". But I think that may be a little inconvenient. So I'm wondering if there are better ways to realize that. And I stumbled over that side:
http://www.javaworld.com/jw-01-1997/jw-01-chat.html?page=6
At the very end it says:
An alternative, however, and much more elegant solution, is to abstract the protocol behind a set of stream classes. The specifics of header construction and insertion can be handled automatically by the stream classes, and the client is then left with much the same interface as before: Clients write messages to a stream, but instead of flushing the stream, they call a method that attaches appropriate headers and sends the encapsulated message.
I don't really know what is meant by that. Could somebody explain it, or even better, give me a code example. Perhaps there may are other ways to do?
Let's say you want to send messages encapsulated as the link you sent:
| ID | len | message contents |.
What they mean with "to abstract the protocol behind a set of stream classes" is to create classes that extend stream classes which will put the correct ID and length on the encapsulated message for you.
For example, for an extended PrintWriter where you send two kinds of message:
ID 1 - normal message.
ID 2 - error message.
class MyProtocolPrintWriter extends PrintWriter {
public static final int NORMAL_MESSAGE = 1;
public static final int ERROR_MESSAGE = 2;
//put the constructor
public void writeMessage(String message) {
println(
String.format(
"%02x%02d%s", NORMAL_MESSAGE, message.length(), message));
}
public void writeErrorMessage(String message) {
println(
String.format(
"%02x%02d%s", ERROR_MESSAGE, message.length(), message));
}
}
Here's what one fairly successful chat network used for a protocol.
Internet Relay Chat
And here's a list of the commands that were implemented using the IRC protocol.
List of Internet Relay Chat commands
You would implement these commands as a set of stream classes. The user issues a command, and your stream class handles the specifics of the header construction and insertion into the stream.

WebSockets - correct approach for creating channels and pushing data

I have a scenario whereby I want to inform users of my site that someone has commented on an article on which they have also commented. This is a lot like the way that SO notifies me when someone responds to a question!
Server side, I persist the comment and then look up all users who commented on the same article. I then broadcast (I'm using Atmosphere):
PushContext pushContext = PushContextFactory.getDefault().getPushContext();
for(User u : users){
// channel name, message
pushContext.push("/user_" + u.id, "someone commented! blah blah");
}
The "channel" I am broadcasting to is the user's "own" channel, since I don't want every user to be notified. I use the user's ID in the channel name to achieve this.
Is this the right way to do ensure only relevant users get notified?
I guess I also want to do two more things:
Only push to users whom I believe are still online. If they aren't online, then it's a waste of resources pushing to them.
Encrypt the message, because otherwise anybody could listen for my messages, if they know my user ID.
Is there anything else I need to think of?
SO uses WebSockets, for example when a comment is made on this posting, you get a notification in your status bar at the top left of the SO page.
When the page is loaded, the browser makes a protocol upgrade request which looks like this:
Request URL:ws://sockets-se.or.stackexchange.com/
Request Method:GET
Status Code:101 Switching Protocols
Request Headersview source
Connection:Upgrade
Cookie:__qca=P0-1697817643-1763440830313; __utma=27376923.959753990.1338240830.1353943751.1384115154.33; __utmc=27693525; __utmz=27699983.1356175156.31.31.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)
Host:sockets-se.or.stackexchange.com
Origin:http://stackoverflow.com
Sec-WebSocket-Extensions:x-webkit-deflate-frame
Sec-WebSocket-Key:6qFl45+6gZ526yMMo79zWQ==
Sec-WebSocket-Version:13
Upgrade:websocket
(Key3):00:00:00:00:00:00:00:00
Response Headersview source
Connection:Upgrade
Sec-WebSocket-Accept:B4h2G+gi78iNZZXg+o6iAztgF1I=
Upgrade:websocket
(Challenge Response):00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
The socket is then open, and the server can send updates to the browser. For example, a notification of my comment is received by the browser as:
{"action":"1-question-12993099","data":"{\"a\":\"comment-add\",\"id\":12993099,\"commentid\":19334206,\"acctid\":1298157}"}
It doesn't contain the actual comment; it appears that this is simply used to tell the browser to show the red icon. When you then click, it makes a request to get the page, including the comment. The question ID (12993099), comment ID (19334206) and account ID (1298157) is contained in that frame.
I cannot see anything in the above which would stop some hacker creating a web socket to listen to your notifications. The cookies look to me to be Google Analytics cookies to me, or at least the second and third ones do. Perhaps the first is some code which you wouldn't know, had I not just published it (no worries, I change it!).
In your example of Atmosphere, I know that when Web Sockets does not work, it defaults to long polling, which then requests a URL with the channel name in it. So you could get the client to generate a channel name which only it would know and associate that with the logged in user. But anyone sniffing the network would again have access to your traffic, so you'd have to secure it using secure web sockets (WSS) and HTTPS (for long polling fallback).

FIX protocol using java

I have developed a utility in Java using the Financial Information eXchange(FIX) protocol to pull data from an input stream. However, I currently have not found any test servers online to which I can connect to that implement the FIX protocol. Could someone please let me know how I can achieve this?
http://www.quickfixj.org/ have an open source fix engine. It comes with an example that has a simple server. You might want to use their library for the client too rather than rebuilding a FIX engine from scratch...
You can use CoralFIX to quickly fire up a test server. It takes care of all the session level FIX messages, such as Logon, ResendRequest, SequenceReset, Heartbeat so you are ready to start exchanging messages with your client. Below a simple example:
import com.coralblocks.coralfix.FixMessage;
import com.coralblocks.coralreactor.client.Client;
import com.coralblocks.coralreactor.nio.NioReactor;
import com.coralblocks.coralreactor.util.Configuration;
import com.coralblocks.coralreactor.util.MapConfiguration;
public class SimpleFixApplicationServer extends FixApplicationServer {
public SimpleFixApplicationServer(NioReactor nio, int port, Configuration config) {
super(nio, port, config);
}
#Override
protected void handleFixApplicationMessage(Client client, FixMessage fixMsg, boolean possDupe) {
// do whatever you want to do with the application message received from this client...
}
public static void main(String[] args) {
NioReactor nio = NioReactor.create();
MapConfiguration config = new MapConfiguration();
// print all messages received and sent to STDOUT for debugging purposes
// (default is false)
config.add("debugMessages", "true");
// accept as the client inbound sequence whatever
// sequence I receive in the first message coming from the client
// (default is false)
config.add("acceptInboundSeqFromClient", "false");
Server server = new SimpleFixApplicationServer(nio, 45451, config);
server.open();
nio.start();
}
}
A full explanation of the code above can be found here.
Disclaimer: I am one of the developers of CoralFIX.
I assume you haven't built it in a vacuum, but rather to talk to some broker's FIX server? If that's the case, speak to your broker as they are likely to have a test server you could connect to.
It depends if you want a hosted or a DIY solution. If you want to quickly bring up a FIX sessions without having to compile/build/setup then I would go for a hosted simulator.
We have an account with these guys http://www.fixsim.com/ and would recommend them (simple to setup and add more connections).
(I don't work for the company and get nothing to say this)
you can try to search more information here: http://fixprotocol.org/discuss/
I work for a broker, and you're more than welcome to connect to our Staging FIX servers. I do not know of any anonymous servers around, perhaps this is something the FIX community could set up..
Cheers,
Chris

How JMS work in Java?

How does async JMS work? I've below sample code:
public class JmsAdapter implements MessageListener, ExceptionListener
{
private ConnectionFactory connFactory = null;
private Connection conn = null;
private Session session = null;
public void receiveMessages()
{
try
{
this.session = this.conn.createSession(true, Session.SESSION_TRANSACTED);
this.conn.setExceptionListener(this);
Destination destination = this.session.createQueue("SOME_QUEUE_NAME");
this.consumer = this.session.createConsumer(destination);
this.consumer.setMessageListener(this);
this.conn.start();
}
catch (JMSException e)
{
//Handle JMS Exceptions Here
}
}
#Override
public void onMessage(Message message)
{
try
{
//Do Message Processing Here
//Message sucessfully processed... Go ahead and commit the transaction.
this.session.commit();
}
catch(SomeApplicationException e)
{
//Message processing failed.
//Do whatever you need to do here for the exception.
//NOTE: You may need to check the redelivery count of this message first
//and just commit it after it fails a predefined number of times (Make sure you
//store it somewhere if you don't want to lose it). This way you're process isn't
//handling the same failed message over and over again.
this.session.rollback()
}
}
}
But I'm new to Java & JMS. I'll probably consume messages in onMessage method. But I don't know how does it work exactly.
Do I need to add main method in JmsAdapter class? After adding main method, do I need to create a jar & then run the jar as "java -jar abc.jar"?
Any help is much appreciated.
UPDATE: What I want to know is that if I add main method, should I simply call receiveMessages() in main? And then after running, will the listener keep on running? And if there are messages, will it retrieve automatically in onMessage method?
Also, if the listener is continuously listening, doesn't it take CPU??? In case of threads, when we create a thread & put it in sleep, the CPU utilization is zero, how doe it work in case of listener?
Note: I've only Tomcat server & I'll not be using any jms server. I'm not sure if listener needs any specific jms server such as JBoss? But in any case, please assume that I'll not be having anything except tomcat.
Thanks!
You need to learn to walk before you start trying to run.
Read / do a tutorial on Java programming. This should explain (among other things) how to compile and run a Java program from the command line.
Read / do a tutorial on JMS.
Read the Oracle material on how to create an executable JAR file.
Figure out what it is you are trying to do ... and design your application.
Looking at what you've shown and told us:
You could add a main method to that class, but to make an executable JAR file, you've got to create your JAR file with a manifest entry that specifies the name of the class with the main method.
There's a lot more that you have to do before that code will work:
add code to (at least) log the exceptions that you are catching
add code to process the messages
add code to initialize the connection factory and connection objects
And like I said above, you probably need some kind of design ... so that you don't end up with everything in a "kitchen sink" class.
if I add main method, should I simply call receiveMessages() in main?
That is one approach. But like I said, you really need to design your application.
And then after running, will the listener keep on running?
It is not entirely clear. It should keep running as long as the main thread is alive, but it is not immediately obvious what happens when your main method returns. (It depends on whether the JMS threads are created as daemon threads, and that's not specified.)
And if there are messages, will it retrieve automatically in onMessage method?
It would appear that each message is retrieved (read from the socket) before your onMessage method is called.
Also, if the listener is continuously listening, doesn't it take CPU???
Not if it is implemented properly.
In case of threads, when we create a thread & put it in sleep, the CPU utilization is zero, how doe it work in case of listener?
At a certain level, a listener thread will make a system call that waits for data to arrive on a network socket. I don't know how it is exactly implemented, but this could be as simple as an read() call on the network socket's InoutStream. No CPU is used by a thread while it waits in a blocking system call.
This link looks like a pretty good place with examples using Oracle AQ. There's an examples section that tells you how to setup the examples and run them. Hopefully this can help.
Link to Oracle Advanced Queueing

Categories

Resources