问题
I am using embedded jetty (group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.4.27.v20200227') and I am trying to programmatically setup it to use JDBC for session storage. All the documentation/examples I can find is about standalone jetty.
Do you know how to setup it?
回答1:
I don't know all that much about JDBC or session storage, but looking at the documentation Persistent Sessions: JDBC for standalone jetty, it is telling you to enable the module session-store-jdbc
. By looking at session-store-jdbc.mod you can see that it uses etc/sessions/jdbc/session-store.xml and these XML files can be directly translated into java code.
So it looks like its adding a JDBCSessionDataStoreFactory
as a bean onto the server. So some equivalent code that you could try would look something like:
// Configure a JDBCSessionDataStoreFactory.
JDBCSessionDataStoreFactory sessionDataStoreFactory = new JDBCSessionDataStoreFactory();
sessionDataStoreFactory.setGracePeriodSec(3600);
sessionDataStoreFactory.setSavePeriodSec(0);
sessionDataStoreFactory.setDatabaseAdaptor(...);
JDBCSessionDataStore.SessionTableSchema schema = new JDBCSessionDataStore.SessionTableSchema();
schema.setAccessTimeColumn("accessTime");
schema.setContextPathColumn("contextPath");
// ... more configuration here
sessionDataStoreFactory.setSessionTableSchema(schema);
// Add the SessionDataStoreFactory as a bean on the server.
server.addBean(sessionDataStoreFactory);
来源:https://stackoverflow.com/questions/60539963/how-to-setup-embeded-jetty-to-use-jdbc-sessions