简体   繁体   English

从Java Application启动和停止RESTful服务

[英]start & stop RESTful service from Java Application

This is a question about implementing a RESTful listener service, but I will provide some background first to provide context. 这是关于实现RESTful侦听器服务的问题,但我将首先提供一些背景知识来提供上下文。

I have a existing java application & swing UI which takes a directory and database file as input. 我有一个现有的java应用程序和swing UI,它将目录和数据库文件作为输入。 After these are set a button imports the PDF forms within that directory into the database. 设置完成后,按钮会将该目录中的PDF表单导入数据库。 There is a small JFrame window in the UI for the buttons and some logging output. UI中有一个小的JFrame窗口用于按钮和一些日志输出。 This works very well. 这非常有效。

I now want this java app to connect to our Exchange Mail Server, listen for new emails with the PDFs attached, save them to a directory and import using my application above. 我现在想要这个Java应用程序连接到我们的Exchange邮件服务器,收听附有PDF的新电子邮件,将它们保存到目录并使用我上面的应用程序导入。

To connect to our exchange server (IMAP, POP etc are not enabled) I was going to use the ews-java API https://github.com/OfficeDev/ews-java-api 要连接到我们的Exchange服务器(IMAP,POP等未启用),我将使用ews-java API https://github.com/OfficeDev/ews-java-api

However this requires that the client who wants to get push notifications from the exchange mail server (via REST) does three things: 但是,这要求希望从Exchange邮件服务器(通过REST)获取推送通知的客户端执行以下三项操作:

1) Connect to the exchange server (I can do this) 1)连接到交换服务器(我可以这样做)

ExchangeService newService = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
newService.setUrl("https://myurl/ews/Exchange.asmx");
ExchangeCredentials credentials = new WebCredentials("user","password");
newService.setCredentials(credentials);

2) Subscribe to push notifications and setup a callback URI (I can do this) 2)订阅推送通知并设置回调URI(我可以这样做)

    @Override
public PushSubscription subscribeToPushNotifications(){

URI callback = null;
PushSubscription pushSubscription = null;
try{
    logger.info("Subscribing to push notifications.. Endpoint Listener URI = " + config.getNotificationListenerURI());
    callback = new URI(config.getNotificationListenerURI());
    pushSubscription = service.subscribeToPushNotifications(
            getFoldersForSubscription(), callback , 5, null, 
            EventType.NewMail, EventType.Created, EventType.Deleted, EventType.Modified, EventType.Moved); 
    logger.info("Done! --> PushSubscription ID= " + pushSubscription.getId());
}catch(URISyntaxException e){
    logger.error("EWS ==> Invalid Notification Listener URL = " + config.getNotificationListenerURI() +". Please, verify <integration-modules>");
    Throwables.propagate(e);
}catch (Exception e){
    logger.error("EWS ==> Error subscribing to push notifications", e);
    Throwables.propagate(e);
}

return pushSubscription;
}

3) Setup a listener service to get new-email notifications (help needed here) 3)设置监听服务以获取新的电子邮件通知(此处需要帮助)

Similar to this : Exchange Web Services Java APi + RESTful Push Notifications Listener 与此类似: Exchange Web服务Java APi + RESTful推送通知监听器

@Path("/emailnotification")
public class ExchangeNotificationListener {

private static final Log logger = LogFactory.getLog(ExchangeNotificationListener.class);

@Path("/incomingevent")
@POST()
@Produces(MediaType.TEXT_XML)
public Response onNotificationReceived() throws Exception {
    logger.info("Received EWS notification !!!");
return Response.ok().build();
}

@GET
@Path("/{param}")
public Response getMsg(@PathParam("param") String msg) {
    String output = "Jersey say : " + msg;
    return Response.status(200).entity(output).build();
    }
}

It appears that my listener and jax-rs needs to run within a container like tomcat for step 3 and these web services that run within tomcat and are started by tomcat. 看来我的监听器和jax-rs需要在像tomcat这样的容器中运行第3步,并且这些在tomcat中运行并由tomcat启动的Web服务。

Is there a way to have my existing application start / stop the listener or do I have to convert the whole thing to be a web service project? 有没有办法让我现有的应用程序启动/停止监听器,还是我必须将整个事件转换为Web服务项目? So far I have only managed to do step 3 as a standalone web project but would like to be able to start stop it from my existing app. 到目前为止,我只是设法将第3步作为一个独立的Web项目,但希望能够从我现有的应用程序开始停止它。

Ideally I would like to have the ExchangeNotificationListener started after the subscription to email notifications in step two. 理想情况下,我希望在第二步订阅电子邮件通知后启动ExchangeNotificationListener。

Could the class that starts the subscription extend the ExchangeNotificationListener class to do this? 启动订阅的类是否可以扩展ExchangeNotificationListener类来执行此操作?

Or should I be using something other than tomcat? 或者我应该使用除tomcat以外的东西?

Thanks 谢谢

I am not aware of any way to run a swing application and a servlet in the same Java process. 我不知道在同一个Java进程中运行swing应用程序和servlet的方法。 Even if there was one it would probably be some kind of hack. 即使有一个它可能是某种黑客。

If you think about it, you will find that there are two completely separate concepts involved. 如果你仔细想想,你会发现有两个完全独立的概念。 Your Swing GUI is a desktop application, and desktop applications usually run in the context of the users session because they require a graphical desktop environment. 您的Swing GUI是桌面应用程序,桌面应用程序通常在用户会话的上下文中运行,因为它们需要图形桌面环境。 So usually they will be started after the user has logged in and closed before the user logs out. 因此,通常在用户登录启动在用户注销之前关闭它们。

On the other hand your "listener" application is a web application, it needs a network connection and maybe file system access but otherwise it knows nothing about a graphical desktop environment. 另一方面,您的“监听器”应用程序是一个Web应用程序,它需要网络连接,并且可能需要文件系统访问,否则它对图形桌面环境一无所知。 Web applications are normally longer running system services that are started when the server is booted and run as long as the server is running. Web应用程序通常是较长时间运行的系统服务,只要服务器正在运行,就会在服务器启动并运行时启动。

If you try to combine them into a single application then that also indicates a problem with your design. 如果您尝试将它们组合到单个应用程序中,那么这也表明您的设计存在问题。

I would instead recommend a different approach. 我会建议采用不同的方法。 Create the three parts for subscription , push-callback-registration and listener inside your web application. 在Web应用程序中为订阅推送回调注册侦听器创建三个部分。 You can deploy that application in a Servlet container, install it as a regular system service and configure it to start it when the system boots up. 您可以将该应用程序部署在Servlet容器中,将其安装为常规系统服务,并将其配置为在系统启动时启动它。

Next you need to think about what you do with mails you receive in the servlet. 接下来,您需要考虑使用servlet中收到的邮件做什么。 Either you store them somewhere and process them when the Swing app is started, or you also move your processing logic to the web application, preferably by moving that code into its own library that you can then access from both the gui application and the servlet. 您可以将它们存储在某个地方并在启动Swing应用程序时处理它们,或者您也将处理逻辑移动到Web应用程序,最好将该代码移动到它自己的库中,然后您可以从gui应用程序和servlet访问它们。

Depending on which option you choose, when you start your Swing application either all your mails would already be in the database and you can simply access them, or you can just process those mails that the servlet has stored while your app was not running. 根据您选择的选项,当您启动Swing应用程序时,要么所有邮件都已存在于数据库中,您只需访问它们,或者您只需处理servlet在您的应用未运行时存储的邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM