How to multithread in Grizzly - java

I'm implementing a Java RESTful webservice on Heroku. Currently my entry point looks like
public class Main
{
public static final String BASE_URI = getBaseURI();
public static void main( String[] args ) throws Exception
{
final Map<String, String> initParams = new HashMap<String, String>();
initParams.put( "com.sun.jersey.config.property.packages", "services.contracts" );
initParams.put( "com.sun.jersey.api.json.POJOMappingFeature", "true" );
System.out.println( "Starting grizzly..." );
SelectorThread threadSelector =
GrizzlyWebContainerFactory.create( BASE_URI, initParams );
System.out.println( String.format( "Jersey started with WADL available at %sapplication.wadl.", BASE_URI,
BASE_URI ) );
}
private static String getBaseURI()
{
return "http://localhost:" + ( System.getenv( "PORT" ) != null ? System.getenv( "PORT" ) : "9998" ) + "/";
}
}
where the initParms contains RESTful services being hosted. I understand that GrizzlyWebContainerFactory.create() returns an instance of ServletContainer (SelectorThread), but how would I multithread the returned threadSelector such that multiple SelectorThread's can handle the incoming requests under one process (aka web dyno)? The reason is to increase performance of a single dyno in handling requests.
Any suggestions appreciated! Thanks!

You can call:
SelectorThread.setSelectorReadThreadsCount(int)

Related

why the method RXTXCommDriver.nativeGetVersion() does not work

I work with the JPOS library. I am trying to get a list of all com-ports using the RXTXCommDriver class. An error occurs when calling the main native method, which gets a list of all ports.
java.lang.UnsatisfiedLinkError: javax.comm.RXTXCommDriver.nativeGetVersion()Ljava/lang/String;
public class RXTXCommDriver implements CommDriver
{
private final static boolean debug = true;
private final static boolean devel = false;
private final static boolean noVersionOutput = "true".equals( System.getProperty( "gnu.io.rxtx.NoVersionOutput" ) );
static
{
if(debug ) System.out.println("RXTXCommDriver {}");
System.loadLibrary( "rxtxSerial" );
/*
Perform a crude check to make sure people don't mix
versions of the Jar and native lib
Mixing the libs can create a nightmare.
It could be possible to move this over to RXTXVersion
but All we want to do is warn people when first loading
the Library.
*/
String JarVersion = RXTXVersion.getVersion();
String LibVersion;
try {
LibVersion = RXTXVersion.nativeGetVersion();
} catch ( Error UnsatisfiedLinkError )
{
// for rxtx prior to 2.1.7
LibVersion = nativeGetVersion();
}
if ( devel )
{
if ( ! noVersionOutput )
{
System.out.println("Stable Library");
System.out.println("=========================================");
System.out.println("Native lib Version = " + LibVersion );
System.out.println("Java lib Version = " + JarVersion );
}
}

Java tomcat application hangs, can't restart tomcat after stopping

I have deployed a mature web application to a new server on tomcat 7. The database the system uses is quite empty as we're in the early stages of configuring it for use.
Going to the app, you get a login page. Log in and it usually takes you to the main page of the app.
But after coming in the following morning, we always get the same problem:
We bring up the login screen - no problem
Enter our username and password - system hangs
We go to tomcat and using
the system tray, stop the service.
The stopping service progress bar appears then goes away, but the status
on the tomcat properties dialog still shows 'Started' and both the Start
and Stop buttons are disabled.
We check the tomcat logs and there are no errors
We restart the server and it works ok again
There is nothing obvious we can see. A tomcat 'Find Leaks' request shows nothing, and looking at the heap sizes on VisualVM shows a consistent pattern of heap takeup followed by garbage collection bringing it back down to the same low level (so no apparent leaks)
I thought it may be mysql connections timing out, but that shouldnt be the case because if I log in with the wrong password, the system goes to the database to check the password and returns as expected with 'wrong password'. The only point at which it fails is if you enter the correct password.
The only clue we have is that there is an error when logging in, where the system uses some custom code to figure out the users' host name:
2019-02-14 08:10:14,277 08:10:14.277 [http-bio-8080-exec-9] ERROR com.sw.app.ui.UserAuthenticatedWebSession - Unknown host!
java.net.UnknownHostException: null
at java.net.Inet6AddressImpl.getHostByAddr(Native Method) ~[na:1.8.0_201]
at java.net.InetAddress$2.getHostByAddr(Unknown Source) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_201]
at com.sw.app.data.utils.NetUtilities.getHostName(NetUtilities.java:114) ~[app-data-4.0.15.1-SNAPSHOT.jar:na]
This is only invoked if the user logs in successfully to store where they are logging in from, but the exception is caught in the code and then just logged rather than propagated upwards, and then we use a default 'unknown' host name. This is the code:
public static String getHostName( InetAddress inaHost ) throws UnknownHostException
{
try {
Class<? extends InetAddress> clazz = Class.forName( "java.net.InetAddress" ).asSubclass( InetAddress.class );
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
constructors[0].setAccessible( true );
InetAddress ina = (InetAddress)constructors[0].newInstance();
Field[] fields = ina.getClass().getDeclaredFields();
for( Field field : fields ) {
// Depends on the version of java we are dealing with:
// Older version - single nameservice
if( field.getName().equals( "nameService" ) ) {
return getHostName( field.get( null ), inaHost );
} else if( field.getName().equals( "nameServices" ) ) {
// newer version - multiple name services possible
StringBuilder builder = new StringBuilder();
field.setAccessible( true );
// A list of nameservice objects
#SuppressWarnings( "unchecked" )
List<Object> nameServices = (List<Object>)field.get( null );
for( Object nameService : nameServices ) {
String hostName = getHostName( nameService, inaHost );
if( builder.length() > 0 ) {
builder.append( ", " );
}
builder.append( hostName );
}
return builder.toString();
}
}
} catch( ClassNotFoundException cnfe ) {
throw new InvalidOperationException( "Class not found when looking up host name", cnfe );
} catch( IllegalAccessException iae ) {
throw new InvalidOperationException( "Cannot access method/field", iae );
} catch( InstantiationException ie ) {
throw new InvalidOperationException( "Cannot instantiate class", ie );
} catch( InvocationTargetException ite ) {
throw (UnknownHostException)ite.getCause();
}
return null;
}
/**
* Get the host name using reflection on the hidden class implementation of the InetAddress details.
* #param p_nameService
* #param p_address
* #return
* #throws IllegalAccessException
* #throws InvocationTargetException
*/
private static String getHostName( Object nameService, InetAddress address ) throws IllegalAccessException, InvocationTargetException {
Method[] methods = nameService.getClass().getDeclaredMethods();
for( Method method : methods ) {
// The nameService is assumed to have a method, getHostByAddr, which takes the byte[] inet address
if( method.getName().equals( "getHostByAddr" ) ) {
method.setAccessible( true );
return (String)method.invoke( nameService, address.getAddress() );
}
}
return "";
}
Does anyone have similar issues?
-- Edit --
Here is the database configuration bean class.
#Configuration
public class AppPersistence {
private static final Logger LOGGER = LoggerFactory.getLogger( AppPersistence.class );
protected static final String INTERNAL_IP_DOMAIN = "*******";
protected static final String JDBC_PROTOCOL = "jdbc:mysql://";
protected static final String DEFAULT_DATABASE_NAME = "*******";
/** The path for context-based property lookups */
protected static final String CONTEXT_LOOKUP_PATH = "java:comp/env";
/** This is the default location for the database - on the same machine as the deployment */
protected static final String DB_LOCAL = JDBC_PROTOCOL + "localhost:3306/" + DEFAULT_DATABASE_NAME;
#Bean
public DataSource createDataSource() throws Exception {
BasicDataSource source = new BasicDataSource();
// allow for parameterised config
source.setDriverClassName( Driver.class.getName() );
source.setUrl( getProperty( "app.database.url", DB_LOCAL ) );
source.setUsername( getProperty( "app.database.username", "*****" ) );
source.setPassword( getProperty( "app.database.password", "****" ) );
LOGGER.warn( "Connecting to: " + source.getUrl() );
return source;
}
protected String getProperty( String name, String default ) {
// first check system properties
String val = System.getProperty( name );
if( val != null ) {
logLookup( "System Properties", name, val );
return val;
}
// check environment variables
val = System.getenv( name );
if( val != null ) {
logLookup( "System Environment Variables", name, val );
return val;
}
// if we are deployed to a container, check the environment variables in that.
try {
Context context = InitialContext.doLookup( "java:comp/env" );
if( context != null ) {
Object valObj = context.lookup( name );
if( valObj != null ) {
logLookup( "Context", name, valObj.toString() );
return valObj.toString();
}
}
} catch( NamingException e ) {
// if running on a dev machine this will probably happen
LOGGER.warn( "Could not find context for lookup of " + p_name + " - assuming running in dev mode with defaults. Error was: " + e.toString( true ) );
LOGGER.info( "Error received on lookup of " + name + ":", e );
}
return p_default;
}
protected void logLookup( String source, String lookup, String value ) {
if( value.contains( "password" ) ) {
// avoid displaying any password info
LOGGER.warn( "Successfully looked up sensitive value from " + source + " for name '" + lookup + "': [******]" );
} else {
LOGGER.warn( "Successfully looked up value from " + source + " for name '" + lookup + "': '" + value + "'" );
}
}
#Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory( DataSource dataSource ) {
LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
entityManagerFactory.setPersistenceUnitName( "com.sw.app.data.persistence" );
entityManagerFactory.setDataSource( dataSource );
entityManagerFactory.setJpaVendorAdapter( new HibernateJpaVendorAdapter() );
entityManagerFactory.setLoadTimeWeaver( new InstrumentationLoadTimeWeaver() );
entityManagerFactory.setJpaDialect( new HibernateJpaDialect() );
entityManagerFactory.setPackagesToScan( "com.sw.app.data", "com.sw.app.rawimport",
"com.sw.app.view", "com.sw.app.warranty" );
entityManagerFactory.setJpaPropertyMap( hibernateJpaProperties( dataSource ) );
return entityManagerFactory;
}
private Map<String, ?> hibernateJpaProperties( DataSource dataSource ) {
HashMap<String, String> properties = new HashMap<>();
// Need to copy these values over, otherwise c3p0 can't see them.
if( dataSource instanceof BasicDataSource ) {
BasicDataSource source = (BasicDataSource)p_dataSource;
properties.put( "hibernate.connection.driver_class", source.getDriverClassName() );
properties.put( "hibernate.connection.url", source.getUrl() );
properties.put( "hibernate.connection.username", source.getUsername() );
properties.put( "hibernate.connection.password", source.getPassword() );
}
// Added to avoid some merge problems when updating entities (eg contact to custimport)
properties.put( "hibernate.event.merge.entity_copy_observer", "allow" );
// Second level cache
properties.put( "hibernate.cache.use_second_level_cache", "true" );
properties.put( "hibernate.cache.use_query_cache", "true" );
properties.put( "hibernate.cache.provider_class", "org.hibernate.cache.EhCacheProvider" );
properties.put( "hibernate.cache.region.factory_class", EhCacheRegionFactory.class.getName() );
properties.put( "hibernate.generate_statistics", "false" );
properties.put( "hibernate.show_sql", "false" );
properties.put( "hibernate.format_sql", "false" );
// validate | update | create | create-drop -->
properties.put( "hibernate.hbm2ddl.auto", "update" );
properties.put( "hibernate.dialect", MySQL5Dialect.class.getName() );
// [main] WARN org.hibernate.cfg.AnnotationBinder - HHH000457: Joined inheritance hierarchy [com.sw.system4.data.collateral.AbstractCollateral] defined explicit #DiscriminatorColumn. Legacy Hibernate behavior was to ignore the #DiscriminatorColumn. However, as part of issue HHH-6911 we now apply the explicit #DiscriminatorColumn. If you would prefer the legacy behavior, enable the `hibernate.discriminator.ignore_explicit_for_joined` setting (hibernate.discriminator.ignore_explicit_for_joined=true) -->
properties.put( "hibernate.discriminator.ignore_explicit_for_joined", "true" );
//properties.put("hibernate.hbm2ddl.import_files", "insert-data.sql");
//properties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
// This wasnt required in persistence.xml, but for some reason is here.
properties.put( "hibernate.connection.provider_class", C3P0ConnectionProvider.class.getName() );
// just adding c3p0 props was enough in persistence.xml, but not here.
properties.put( "hibernate.c3p0.min_size", "5" );
properties.put( "hibernate.c3p0.max_size", "20" );
properties.put( "hibernate.c3p0.timeout", "300" ); // 5mins
properties.put( "hibernate.c3p0.max_statements", "50" );
properties.put( "hibernate.c3p0.idle_test_period", "100" );
properties.put( "hibernate.c3p0.preferredTestQuery", "select 1" );
properties.put( "hibernate.c3p0.testConnectionOnCheckout", "true" );
properties.put( "hibernate.c3p0.numHelperThreads", "12" );
properties.put( "hibernate.c3p0.maxStatementsPerConnection", "25" );
properties.put( "hibernate.c3p0.statementCacheNumDeferredCloseThreads", "1" );
return l_properties;
}
#Bean
public JpaTransactionManager transactionManager( EntityManagerFactory emf ) {
JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
jpaTransactionManager.setEntityManagerFactory( emf );
return jpaTransactionManager;
}
}
What is your database? Is it a Cloud Database? I had a similar problem with CloudSQL. What happened was when some active connections didn't do anything with the database, from the database side, it rejects the connection after some hours. But on the application side, you will see it as an active connection. I have used Apache DBCP pool, and I was able to solve the problem using this in the database configurations.
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestOnBorrow(true);
dataSource.setTestWhileIdle(true);
Because you are using C3P0, following commands should work for you.
hibernate.dbcp.testOnBorrow=true
hibernate.dbcp.testOnReturn=true
hibernate.dbcp.validationQuery=SELECT 1

Equinox P2 IStatus returns code 10001 in Eclipse RAP application "Your original request has been modified. null children=[]"

I have to work with an already existing Eclipse RAP application which contains of two features and around 40 dependencies.
Since the Eclipse RAP doesn't have a preStartup() method I call the update procedure in the RAP's start() method:
public class MyApplication implements IApplication
{
#Override
public Object start( IApplicationContext context ) throws Exception
{
P2Util.update();
...
}
}
The IProvisioningAgent is not null, the IMetadataRepositoryManager and IArtifactRepositoryManager are correctly initialized.
public static boolean configureRepository( IProvisioningAgent agent )
{
String repo = "file:///c:/export/repository/"; // TODO HERE!
log.debug( "Initiliazing Repository Managers" );
IMetadataRepositoryManager metadataManager = ( IMetadataRepositoryManager ) agent.getService( IMetadataRepositoryManager.SERVICE_NAME );
IArtifactRepositoryManager artifactManager = ( IArtifactRepositoryManager ) agent.getService( IArtifactRepositoryManager.SERVICE_NAME );
URI uri;
try
{
uri = new URI( repo );
}
catch ( URISyntaxException e1 )
{
log.error( "Unexpected URISyntaxException, the specified path is not a valid URI", e1 ); //$NON-NLS-1$
return false;
}
if ( metadataManager == null )
{
log.error( "IMetadataRepositoryManager instance is null!" );
return false;
}
metadataManager.addRepository( uri );
log.debug( "Added repository to MetadataManager: " + repo );
if ( artifactManager == null )
{
log.error( "IArtifactRepositoryManager instance is null!" );
return false;
}
artifactManager.addRepository( uri );
log.debug( "Added repository to ArtifactManager: " + repo );
return true;
}
Still one problem occurs all the time after I call checkForUpdates().
public static IStatus checkForUpdates( IProvisioningAgent agent ) throws OperationCanceledException
{
log.info( "Checking for new updates in repository" );
ProvisioningSession session = new ProvisioningSession( agent );
UpdateOperation operation = new UpdateOperation( session );
IStatus status = operation.resolveModal( new NullProgressMonitor() );
return status;
}
It returns the following status:
Status OK: org.eclipse.equinox.p2.operations code=10001 Your original request has been modified. null children=[]
I have already checked the flag "Support software installation in the launched application" in my run configuration, the problem persists.

Neo4j embedded only stores inside one runtime

I'm experimenting with Neo4J via Embedded Java API.
My Build path seems ok (no Exceptions during runtime).
When I create some nodes and relations, I can query it directly after it with success.
But after shutting down and re-run my programm, i'm only getting the data I created in the new runtime and none of them before.
But if I look at my directory, I see, that the size has grown with each runtime, I perform a creating of data.
Here's my code:
public static void main(String[] args)
{
GraphDatabaseService gdb = new GraphDatabaseFactory().newEmbeddedDatabase( "/mytestdb/" );
create( gdb );
query( gdb );
gdb.shutdown();
}
private static void query( GraphDatabaseService gdb )
{
StringLogger sl = StringLogger.wrap( new Writer()
{
#Override
public void write( char[] arg0, int arg1, int arg2 ) throws IOException
{
for( int i=arg1; i<=arg2; i++ ) System.out.print( arg0[i] );
}
#Override
public void flush() throws IOException
{}
#Override
public void close() throws IOException
{}
} );
ExecutionEngine ee = new ExecutionEngine( gdb, sl );
ExecutionResult result = ee.execute( "MATCH (p:Privilleg) RETURN p" );
System.out.println( result.dumpToString() );
}
private static void create( GraphDatabaseService gdb )
{
Transaction tx = gdb.beginTx();
Node project = gdb.createNode( MyLabels.Project );
Node user = gdb.createNode( MyLabels.User );
Node priv1 = gdb.createNode( MyLabels.Privilleg );
Node priv2 = gdb.createNode( MyLabels.Privilleg );
user.setProperty( "name", "Heinz" );
user.setProperty( "email", "heinz#gmx.net" );
priv1.setProperty( "name", "Allowed to read all" );
priv1.setProperty( "targets", Short.MAX_VALUE );
priv1.setProperty( "read", true );
priv1.setProperty( "write", false );
priv2.setProperty( "name", "Allowed to write all" );
priv2.setProperty( "targets", Short.MAX_VALUE );
priv2.setProperty( "read", false );
priv2.setProperty( "write", true );
project.setProperty( "name", "My first project" );
project.setProperty( "sname", "STARTUP" );
user.createRelationshipTo( priv1, MyRelationships.UserPrivilleg );
user.createRelationshipTo( priv2, MyRelationships.UserPrivilleg );
priv1.createRelationshipTo( project, MyRelationships.ProjectPrivilleg );
priv2.createRelationshipTo( project, MyRelationships.ProjectPrivilleg );
tx.success();
}
Your code doesn't close the transaction. Typically you use a try-with-resources block:
try (Transaction tx=gdb.beginTx()) {
// do stuff in the graph
tx.success();
}
Since Transaction is AutoClosable its close() method will be called implicitly upon leaving the code block. If (for whatever) reason you decide not to use try-with-resources, be sure to explicitly call close().
On a different notice: your code uses ExecutionEngine. Since Neo4j 2.2 you directly call gdb.execute(myCypherString) instead.
Thank you! This works.
Also, before I closed the transaction, it takes about 20 seconds to shuting down the db. This also is now less than a second.

Java Simple Email Program in Eclipse

I want to make a simple program where you can send email from the Command Line. I found this tutorial, ' http://www.tutorialspoint.com/java/java_sending_email.htm ', however the downloads don't. So where can I get JavaMail API and Java Activation Framework (JAF) and how would i put it in my class path.
Basically Im looking for someone to break it down and show me how I could make an email program.
Im using Eclipse luna.
Do have a look at this example. This example, simply sends one attachment as a mail. The contents of attachment quiz.txt are as follows:
What is the Capital of India?/New Delhi
Where is the Taj Mahal?/Agra
Here is the SendMailExample.java file:
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class SendMailExample {
private String from;
private String to;
private String subject;
private String messageBody;
private String fileName;
private String host;
private Properties properties;
private MimeMessage message;
private BodyPart messageBodyPart;
private Multipart multipart;
private Authenticator authenticator;
public SendMailExample () {
from = "sender#gmail.com";
to = "recipient#gmail.com";
subject = "Subject Testing";
messageBody = "<html><body><h1>HAVE FAITH, AND STAY" +
" CALM :-) I AM WITH YOU, OKAY :-)</h1></body></html>";
fileName = "quiz.txt";
host = "smtp.gmail.com";
authenticator = new SMTPAuthenticator ();
properties = System.getProperties ();
properties.put ( "mail.smtp.host", host );
properties.put ( "mail.smtp.starttls.enable", "true" );
properties.put ( "mail.smtp.port", "587" );
properties.put ( "mail.smtp.auth", "true" );
}
private void sendMail ( String from, String to,
String subject, String messageBody, String fileName ) {
try {
Session session = Session.getDefaultInstance ( properties, authenticator );
message = new MimeMessage ( session );
message.setFrom ( new InternetAddress ( from ) );
message.addRecipient ( Message.RecipientType.TO,
new InternetAddress ( to ) );
message.setSubject ( subject );
multipart = new MimeMultipart ();
messageBodyPart = new MimeBodyPart ();
messageBodyPart.setContent ( messageBody, "text/html" );
multipart.addBodyPart ( messageBodyPart );
messageBodyPart = new MimeBodyPart ();
DataSource source = new FileDataSource ( fileName );
messageBodyPart.setDataHandler ( new DataHandler ( source ) );
messageBodyPart.setFileName ( fileName );
multipart.addBodyPart ( messageBodyPart );
message.setContent ( multipart );
Transport.send ( message );
System.out.println ( "Message send successfully...." );
} catch ( Exception me ) {
me.printStackTrace ();
}
}
private void performTask () {
sendMail ( from, to, subject, messageBody, fileName );
}
public static void main ( String[] args ) {
new SendMailExample ().performTask ();
}
}
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
class SMTPAuthenticator extends Authenticator {
private static final String SMTP_AUTH_USER = "example#gmail.com";
private static final String SMTP_AUTH_PASSWORD = "somepassword";
public PasswordAuthentication getPasswordAuthentication () {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PASSWORD;
return new PasswordAuthentication( username, password );
}
}
You simply needed this mail.jar file.
To compile, simply write ( mail.jar is present at C:\install\java\mail\mail.jar` location ):
javac -classpath .;C:\install\java\mail\mail.jar SendMailExample.java
To run, write:
java -classpath .;C:\install\java\mail\mail.jar SendMailExample
THis will do :-)
Take a look at this library Commons Email it will simplify your task

Categories