sending message from xmpp server vysper in java - java

I have set up simple server and client, but i don't no how to send message from xmpp server to client. Please give me some help. If possible then suggest me some links.

This is a question which surprisingly often comes up for Vysper. There are several reasons for even asking the question, one particular reason I think is that a HTTP web server in fact works in such a way that it creates and sends content (HTML, CSS etc.) to the agent a.k.a. web browser.
In message-based protocols like email and chat this is a bit different.
Emails are created and consumed by the agents a.k.a. the email clients. Servers mostly only act as Message Brokers (http://en.wikipedia.org/wiki/Message_broker), including aspects like authentication, filtering, storing etc. Rarely do they produce their own email messages out of themselves. Often, a few central accounts (e.g. order#acme.com, support#acme.com) create most of the emails, meaning that the actual messages are produced by an email client and delivered by the server on behalf of the client. (Additionally, email/SMTP has the specialty that clients send out email directly to the receiver's email server which is a nightmare going by the name of /spam/.)
In general, XMPP is no difference here. XMPP chat clients connect and send out and receive messages. The XMPP server brokers the messages. So, to answer your question, in most cases it is sufficient and suggested to have a central account communicating with all the other accounts. It's the simplest and best solution.
However, XMPP offers a little bit more than chat. It has extensions for wizzard-like workflows based on forms, publish/subscribe and administation/commands.
You can add your own extension, if you really need to:
For example, have a look at the VCard extension here: http://svn.apache.org/viewvc/mina/vysper/trunk/server/core/src/main/java/org/apache/vysper/xmpp/modules/extension/xep0054_vcardtemp/
Foremost, I'd recommend to subclass org.apache.vysper.xmpp.modules.core.base.handler.DefaultIQHandler This is like implementing your own Servlet by subclassing DefaultServlet. It contains the XMPP stanza logic you want to provide.
Additionally, you need to plug your handler into the server. This is best done by following the example in VcardTempModule, which
provides a Handler instance and registers it
initializes persistence (or any other backend connection you might need)
makes your extension's namespace known and announces your IQ stuff in the Service Discovery
If you need persistence, have a look at the VcardTempPersistenceManager.
What remains to be done is to make your Module known to the server. If you use Spring, add one line to the Spring configuration. If you use an embedded approach, you'd need to call the equivalent to server.addModule(new VcardTempModule()); like it is done in org.apache.vysper.xmpp.server.ServerMain
Now, if you'd want to emit new Stanzas (messages) which are not a reaction to other Stanzas going through the server, you'd also need to start off your own Thread which is able to create and send Stanzas.
But again, the preferred way is to have the clients create all messages.

Related

What kind of message formats are usually being exchanged in Message Queueing Systems?

I am trying to understand the basics of Message Queues. I see that there are many implementations available as libraries for MQs (ActiveMQ, RabbitMQ, ZeroMQ etc). Also J2EE enabled servers provide such support I think.
What I fail to understand about the topic, is how are these kind of constructs used by real software. I mean what kind of messages are usually being exchanged? Strings? Binary data?
If I understand correctly one can configure the transport protocol, but what is usually the application data format?
Is it a new way of communication, like e.g. SOAP WS or REST WS or RPC etc where each has a different application msg format?
Message queues usually using for application integration. In enterprise it is usually used to implement ESB, but nowadays there are smaller application systems that utilize similar patterns.
Concerning data being transmitted - usually it is XML messages, but actually depends on applications and MQ software - some of them are able to handle binary messages, some are not.
For example imagine you have two applications which require data interchange. If you integrate them using some kind of messaging software such as ActiveMQ, for example, then it will give you some benefits like routing, fault-tolerance, balancing, etc, out-of-the-box.
You may integrate your applications using MQ directly, but ESBs usually
give you ability to use web services: app just calls ws of ESB and knows nothing about underlying architecture.
Also MQs and ESBs gives you a level of abstraction: you may switch your apps in a system absolutely transparent, as long as data exchange interface is preserved.
MQs are mainly used for interprocess communication, or for inter-thread communication within the same process. They provide an asynchronous communications protocol, meaning that the sender and receiver of the message do not need to interact with the message queue at the same time. Messages placed onto the queue are stored until the recipient retrieves them.
wikipedia can be good intro to topic. http://en.wikipedia.org/wiki/Message_queue#Standards_and_protocols
Also, to understand diff between webservice and mq, read this thread: Message Queue vs. Web Services?

Framework or design pattern for mailing all users of a webapp

My app takes care of user registration (with the option to receive email announcements), and can easily handle the actual template-based rendering of email for a given user. JavaMail provides the mail transport layer. But how should I design the application layer between the business objects (e.g. User) and the mail transport?
The straightforward approach would be a simple, synchronous loop: iterate through the users, queue the emails, and be done with it. "Queue" might mean sending them straight to the MTA (mail server), or to an in-memory queue to be consumed by another thread.
However, I also plan to implement features like throttling the rate of emails, processing bounced emails (NDRs), and maintaining status across application restarts. My intuition is that a good design would decouple this from both the business layer and the mail transport layer as much as possible. I wondered if others had solved this problem before, but after much searching I haven't found any Java libraries which seem to fit this problem. Standalone mail apps such as James or list servers are too large in scope; packages like Spring's MailSender or Commons Email are too small in scope (being basically drop-in replacements for JavaMail). For other languages I haven't found anything appropriate either.
I'm curious about how other developers have gone about adding bulk mailing to their applications.
The approach I've been happiest with is to provide an interface to my application to "send" mails. In reality, the implementation of this interface simply queues the mail into a database for later processing. From the application's perspective, this interface is fast, as it performs very little actual work. Plus, the persistence survives server downtime, as you mentioned.
Another thread reads from the queue and takes it's sweet time sending mail up to it's configured throttle, and flags messages in the queue after successfully processing them (effectively dequeuing them without deleting them). This provides both a history of sent mail, and a reference when mail is bounced, etc. I delete from the queue 7 days after a successful send.
In terms of decoupling the solution from the mail transport layer... I've applied this approach to an automated Twitter client, and found it to be equally successful.
One option is to use a hardware appliance. My company uses Strongmail, at least for marketing communications: http://www.strongmail.com/index.php. I don't know much about it, but I think it handles bulk e-mail concerns like do-not-contact lists, throttling, avoiding getting spam filtered, etc.

Asynchronous email processing in Java web application

I would like to implement asynchronous email sending in my web application when users register for a new account. This is so that if there is a problem or delay in sending the email message (e.g. the mail server is down or the network connection to the mail server is slow) the user won't be kept waiting for the sending to complete.
My web app is built using Spring and Hibernate's implementation of JPA.
What would be the best and most reliable way for me to implement asynchronous email processing in this web application?
I am thinking about persisting the email information in a database table which is then regularly polled by a Quartz (http://www.opensymphony.com/quartz/) scheduled job for updates and when it finds new unsent emails, it attempts to send them.
Is this a reasonable way of implementing what I want?
Thanks.
Edit:
The most voted on response is to leave the sending of mail as a synchronous call but what's triggered my thinking that an asynchronous approach might be best is that I'm currently using GMail as my outbound mail server (this is for testing while developing) and I'm experiencing a 25 second delay in response from when my app tries to send the email to when the call to the mail send function returns. What do you think?
I would suggest that you don't bother. Most Unix-style MTAs invented and perfected deferred sending decades ago, and you shouldn't be reinventing the wheel. You'll do it poorly (in comparison to sendmail or postfix), and you'll miss something. My best advice is to use the Java Mail APIS javax.mail and let the MTA deal with the asynchronous part.
You can implement queueing by hand, using MySQL or some other persistent mechanism, but you could also use JMS for queueing. It's pretty much a perfect match for just this kind of situations.
In that case I would be tempted by splitting the mail component off from the main app, and let the two communicate using JMS. The main app puts a message in the JMS and the mailer app would subscribe to the queue and try to process the messages.
JMS can be made persistent (to e.g. MySQL) pretty easily by configuration.
The advantage of spliting the webapp is that you abstract away the notification mechanism and could in the future implement e.g. Google Wave or IRC or whatever without having to touch your main app.
Someone else suggested to use a postfix or sendmail and let them handle the queueing. That is also a great solution, especially if you put the postfix or sendmail on localhost and let it route the messages further. Do try to configure that mailer program such that only mail from localhost is allowed to be routed, to prevent creating an open mailer :)
EDIT clarified the use of JMS + comment on local mailer daemon
It is fairly reasonable, this is the kind of thing Quartz was build for.
However, note that you don't need to schedule through the database at all (unless the server downtime is a real issue). You can simply schedule a Quartz job without database access (the simplest Quartz examples show you how).
Otherwise, if you do choose the database access you have the possibility of sending the emails from another application entirely (a nice thing if you need it).
Java EE 6 makes this so easy with the #Asynchronous annotation.

Using JMS or ThreadPool to send email messages

I will like to know:
I have a scenario. If a user adds a product to the system (I'm developing), there's a listener that sends a notification to the user's client base notifying of a new product added by the user.
I've read this thread and (seeing I've never used JMS nor ThreadPool before) I was wondering whether I should use JMS or ThreadPooling.
I am using Tomcat 5.5 and higher and JBoss 5 and higher (depending on company last resort) to deploy my web application.
If I use JMS, do I use Apache ActiveMQ or JBoss Messaging? Are they both compatible to run on both platforms (Tomcat and JBoss)?
Thanks in advance.
For communicating between applications, JMS is a very good solution, especially for events and notifications. JMS allows for such notifications to be sent and received using what is known as asynchronous messaging whereby the sender and receiver have no knowledge of one another and no requirement to be available at the same time.
ActiveMQ is a very widely used message broker that provides client APIs for Java, C/C++, C#, Perl, PHP, Python, Ruby and more. This allows the use of JMS with applications written in Java and other languages.
I have implemented JMS messaging many, many times for a large variety of business situations to handle events and notifications. The vast majority of these times, I have recommended and/or used Spring JMS no matter what message broker is being used. Spring JMS is incredibly easy to use, extremely robust and highly scalable. Spring JMS removes the complexity of creating your own message producers and message consumers, which can save you a tremendous amount of time.
To see how easy it is to send messages using Spring JMS, check out a blog post I wrote recently titled Using the Spring JmsTemplate to Send JMS Messages. I'm also working on a blog post about receiving messages using Spring JMS.
If you have any further questions, let me know.
Bruce
I had a similar requirement once, and we used JMS. Then main problem was how to deal with errors because SMTP is indeed not transactional:
is it ok if some email are lost?
is it ok if some email are sent twice?
We decided it was better to send the message twice, and here is more or less the design we had:
We relied on container managed transaction and if for some reason the email can not be sent, we decided to rollback the JMS transaction; the message would be redelivered later by JMS and an new attempt to send the message was done.
If the JMS message delivery transaction failed after the email was sent (e.g. because of a problem with JMS), the transaction would be rolled back automatically and the message was redelivered later. In this case, the email was sent twice because STMP is not transactional.
Even if the email can be sent (from point of view of code), the SMTP server can still have problem later. In this case, the JMS have been delivered and consumed, so we had no way to know which email had been processed and how to re-send them manually.
But we were already using JMS. I would not introduce JMS just for that given that the main argument is that JMS is transactional, but SMTP isn't anyway.
I would go for something lighter -- possibly with a ThreadPool -- and store the state in a database to know which email need to be sent or has been sent. If there are some problem, you can look at the database and take the ad-hoc decisions.
I know that this reply is very late to this dicsussion, but I hope it will still be valuable for folks seeking info on integrating ActiveMQ and Tomcat.
I've had many people ask me for help with issues they have had integrating ActiveMQ and Tomcat so I decided to write some articles about it. Not only is this topic covered in ActiveMQ in Action (see chapter 8), but I also published a series of articles on it titled ActiveMQ and Tomcat: Perfect Partners. Hopefully people will find this helpful.
I would go for a persistent JMS (I have used only WLS JMS and Websphere MQ so can't compare AQ vs JBoss, whichever offers a better guarantee for delivery). Also, I would seriously consider making the email engine a completely separate application, depending on how much you expect the traffic to grow.

Real world use of JMS/message queues? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I was just reading abit about JMS and Apache ActiveMQ.
And was wondering what real world use have people here used JMS or similar message queue technologies for ?
JMS (ActiveMQ is a JMS broker implementation) can be used as a mechanism to allow asynchronous request processing. You may wish to do this because the request take a long time to complete or because several parties may be interested in the actual request. Another reason for using it is to allow multiple clients (potentially written in different languages) to access information via JMS. ActiveMQ is a good example here because you can use the STOMP protocol to allow access from a C#/Java/Ruby client.
A real world example is that of a web application that is used to place an order for a particular customer. As part of placing that order (and storing it in a database) you may wish to carry a number of additional tasks:
Store the order in some sort of third party back-end system (such as SAP)
Send an email to the customer to inform them their order has been placed
To do this your application code would publish a message onto a JMS queue which includes an order id. One part of your application listening to the queue may respond to the event by taking the orderId, looking the order up in the database and then place that order with another third party system. Another part of your application may be responsible for taking the orderId and sending a confirmation email to the customer.
Use them all the time to process long-running operations asynchronously. A web user won't want to wait for more than 5 seconds for a request to process. If you have one that runs longer than that, one design is to submit the request to a queue and immediately send back a URL that the user can check to see when the job is finished.
Publish/subscribe is another good technique for decoupling senders from many receivers. It's a flexible architecture, because subscribers can come and go as needed.
I've had so many amazing uses for JMS:
Web chat communication for customer service.
Debug logging on the backend. All app servers broadcasted debug messages at various levels. A JMS client could then be launched to watch for debug messages. Sure I could've used something like syslog, but this gave me all sorts of ways to filter the output based on contextual information (e.q. by app server name, api call, log level, userid, message type, etc...). I also colorized the output.
Debug logging to file. Same as above, only specific pieces were pulled out using filters, and logged to file for general logging.
Alerting. Again, a similar setup to the above logging, watching for specific errors, and alerting people via various means (email, text message, IM, Growl pop-up...)
Dynamically configuring and controlling software clusters. Each app server would broadcast a "configure me" message, then a configuration daemon that would respond with a message containing all kinds of config info. Later, if all the app servers needed their configurations changed at once, it could be done from the config daemon.
And the usual - queued transactions for delayed activity such as billing, order processing, provisioning, email generation...
It's great anywhere you want to guarantee delivery of messages asynchronously.
Distributed (a)synchronous computing.
A real world example could be an application-wide notification framework, which sends mails to the stakeholders at various points during the course of application usage. So the application would act as a Producer by create a Message object, putting it on a particular Queue, and moving forward.
There would be a set of Consumers who would subscribe to the Queue in question, and would take care handling the Message sent across. Note that during the course of this transaction, the Producers are decoupled from the logic of how a given Message would be handled.
Messaging frameworks (ActiveMQ and the likes) act as a backbone to facilitate such Message transactions by providing MessageBrokers.
I've used it to send intraday trades between different fund management systems. If you want to learn more about what a great technology messaging is, I can thoroughly recommend the book "Enterprise Integration Patterns". There are some JMS examples for things like request/reply and publish/subscribe.
Messaging is an excellent tool for integration.
We use it to initiate asynchronous processing that we don't want to interrupt or conflict with an existing transaction.
For example, say you've got an expensive and very important piece of logic like "buy stuff", an important part of buy stuff would be 'notify stuff store'. We make the notify call asynchronous so that whatever logic/processing that is involved in the notify call doesn't block or contend with resources with the buy business logic. End result, buy completes, user is happy, we get our money and because the queue is guaranteed delivery the store gets notified as soon as it opens or as soon as there's a new item in the queue.
I have used it for my academic project which was online retail website similar to Amazon.
JMS was used to handle following features :
Update the position of the orders placed by the customers, as the shipment travels from one location to another. This was done by continuously sending messages to JMS Queue.
Alerting about any unusual events like shipment getting delayed and then sending email to customer.
If the delivery is reached its destination, sending a delivery event.
We had multiple also implemented remote clients connected to main Server. If connection is available, they use to access the main database or if not use their own database. In order to handle data consistency, we had implemented 2PC mechanism.
For this, we used JMS for exchange the messages between these systems i.e one acting as coordinator who will initiate the process by sending message on the queue and others will respond accordingly by sending back again a message on the queue.
As others have already mentioned, this was similar to pub/sub model.
I have seen JMS used in different commercial and academic projects. JMS can easily come into your picture, whenever you want to have a totally decoupled distributed systems. Generally speaking, when you need to send your request from one node, and someone in your network takes care of it without/with giving the sender any information about the receiver.
In my case, I have used JMS in developing a message-oriented middleware (MOM) in my thesis, where specific types of object-oriented objects are generated in one side as your request, and compiled and executed on the other side as your response.
Apache Camel used in conjunction with ActiveMQ is great way to do Enterprise Integration Patterns
We have used messaging to generate online Quotes
We are using JMS for communication with systems in a huge number of remote sites over unreliable networks. The loose coupling in combination with reliable messaging produces a stable system landscape: Each message will be sent as soon it is technically possible, bigger problems in network will not have influence on the whole system landscape...

Categories

Resources